Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is my code
public MainWindow()
{
    InitializeComponent();
    StringBuilder sb = new StringBuilder();
    sb.Append(@"<Thumb x:Name='thumb1' Canvas.Left='0' Canvas.Top='0' Canvas.ZIndex='99' DragDelta='Thumb_DragDelta' >
        < Thumb.Template >
            < ControlTemplate >
                < Image Source = 'images/table1.jpg' Name = 't1' Stretch = 'None' />
            </ ControlTemplate >
        </ Thumb.Template >
    </ Thumb > ");
    UIElement myThumb = (UIElement)XamlReader.Parse(sb + "");
    Canvas.Children.Add(myThumb);

I GET THIS ERRORMESSAGE :

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

Additional information: 'x' is an undeclared prefix. Line 1, position 8.

What I have tried:

I try to create some code in c# that will put some xaml controls on my application so i can put it in a foreach loop and check the database.
Posted
Updated 28-Apr-16 14:08pm

1 solution

From the error I suspect the problem is that you haven't specified the the XML namespace corresponding to the x prefix. You could try something like:
C#
public MainWindow()
{
  InitializeComponent();
  const string thumbXaml = 
    "<Thumb x:Name='thumb1' 
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            Canvas.Left='0'
            Canvas.Top='0'
            Canvas.ZIndex='99'
            DragDelta='Thumb_DragDelta' >
        <Thumb.Template>
            <ControlTemplate>
                <Image Source = 'images/table1.jpg' Name = 't1' Stretch = 'None' />
            </ControlTemplate>
        </Thumb.Template>
     </Thumb>";
    UIElement myThumb = (UIElement)XamlReader.Parse(thumbXaml);
    Canvas.Children.Add(myThumb); // the name of your Canvas element is "Canvas"??

You certainly need the xmlns:x= attribute, and probably also the xmlns= attribute. I suspect it will still fail because it has no way of knowing about the context so it can locate the Thumb_Drag_Delta event handler method that, I suspect, is in the MainWindow code behind.
(And the "Name" attribute in the Image element probably should be "x:Name".)

OK. First of all, since this is all static, why not just put it into the XAML file for the MainWindow?

But, maybe this is just "simplified for an example"...fair enough.

Create the controls in code, set the appropriate values and then hook 'em all up.
C#
public MainWindow()
{
  InitializeComponent();
  Thumb th = new Thumb();
  th.AddHandler(Thumb.DragDeltaEvent, this.Thumb_DragDelta);
  ControlTemplate thCt = new ControlTemplate();
  // fill in the ControlTemplate here....
  th.Template = thCt;
  myCanvas.Children.Add(th);  // I renamed this for disambiguation re: the Canvas class itself.
  th.SetValue(Canvas.LeftProperty, 0);  // MTH: Fixed the property names...
  th.SetValue(Canvas.TopProperty, 0);
  th.SetValue(Canvas.ZIndexProperty, 99);
  // etc...

(Caveat: I didn't try any of this...!)
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 29-Apr-16 9:04am    
This is how it can be done:


UIElement someElement = new ... // concrete class
//...
Canvas.SetLeft(someElement, left);
Canvas.SetTop(someElement, top);
//...

This is because Left, Top, etc. are dependency properties of the UIElement, they depend on logical parent.

—SA
Matt T Heffron 29-Apr-16 13:19pm    
Would the
Canvas.SetLeft(someElement, 0);
be equivalent to my
th.SetValue(Canvas.Left, 0);
(where my th corresponds to your someElement)?

(And shouldn't your MyCanvas. be Canvas. as it is referencing the static method?)
Sergey Alexandrovich Kryukov 29-Apr-16 13:28pm    
I thin not. Canvas.Left simply means the location of Canvas itself, relative to its parent, and this is not what we need. We need to set Left of a child element relative to the Canvas. This term explains it all: "Dependency Property". What I show works as expected; I always do it here and there.

And sorry for my mistake; I really forgot these methods are static and wrote from my memory (broken at this point :-). I'll fix my comment above.

—SA
Matt T Heffron 29-Apr-16 13:38pm    
I see I had a "think-o" ;-)
I should have used Canvas.LeftProperty, etc.
Would that be equivalent to your code above?
Sergey Alexandrovich Kryukov 29-Apr-16 13:56pm    
Maybe, but why? The ideology is to use dependency properties and not the DependencyProperty objects. Can you see the point? Canvas.LeftProperty is not a property. This is one of the fields referencing Dependency Property infrastructure element; its type is DependencyProperty, some type used to implement the mechanism of dependency properties, and the field itself is one of the Canvas's attached properties.

Let me tell you this: both dependency properties and the attached properties are related to one special way to implement the normal property mechanism needed just in XAML. The build complies XAML into source code, say, in C#. That build step is, of course, agnostic to what the property names mean. So, it needs some lookup mechanism to find out: what is the location of the property data to be used by the setter or getter, if XAML tells only the name of the property? Essentially, this is all why all this machinery is used.

In the code behind, we use all those properties as regular properties, directly through property syntax or through the methods like Canvas.SetLeft. The getters are settes are defined in those properties is some special way, but it does not change how they are called in code. Using DependencyProperty objects in code behind would be totally pointless.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900