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:
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);
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.
public MainWindow()
{
InitializeComponent();
Thumb th = new Thumb();
th.AddHandler(Thumb.DragDeltaEvent, this.Thumb_DragDelta);
ControlTemplate thCt = new ControlTemplate();
th.Template = thCt;
myCanvas.Children.Add(th);
th.SetValue(Canvas.LeftProperty, 0);
th.SetValue(Canvas.TopProperty, 0);
th.SetValue(Canvas.ZIndexProperty, 99);
(Caveat: I didn't
try any of this...!)