Minh Tài Phạm
Minh Tài Phạm

Reputation: 23

Template doesnt work in Content Control in C#

Here is my xaml

        <Canvas x:Name="DesignArea">
        <ContentControl
               Name="DesignerItem"
               Width="100"
               Height="100"
               Canvas.Top="100"
               Canvas.Left="100"
               Template="{StaticResource DesignerItemTemplate}">
            <Ellipse Fill="Blue" IsHitTestVisible="False"/>
        </ContentControl>
        <ContentControl Width="130"
                MinWidth="50"
                Height="130"
                MinHeight="50"
                Canvas.Top="150"
                Canvas.Left="150"
                Template="{StaticResource DesignerItemTemplate}">
            <Path Fill="Blue"
        Data="M 0,5 5,0 10,5 5,10 Z"
        Stretch="Fill"
        IsHitTestVisible="False"/>
        </ContentControl>
    </Canvas>

As you can see that the content control use the template just fine with all of it functionality But I want to do it through C# like this

            ContentControl ct = new ContentControl();
        ControlTemplate Temp;
        Temp = (ControlTemplate)this.FindResource("DesignerItemTemplate");
        ct.Template = Temp;
        Ellipse ell = new Ellipse();
        ell.Fill = new SolidColorBrush(Colors.Black);
        ell.Width = 100;
        ell.Height = 100;
        ell.IsHitTestVisible = false;
        ct.Content = ell;
        DesignArea.Children.Add(ct); 

The Black Ellipse in C# did show up the grid template like the xaml shape But I can't move, drag drop, resize or rotate Like the content control in the canvas What happen?

screenshot

Upvotes: 0

Views: 141

Answers (1)

Minh T&#224;i Phạm
Minh T&#224;i Phạm

Reputation: 23

Oh I see why Turn out I need to set the properties of the Canvas too in order to make it work Canvas.SetLeft(ct, 300); Canvas.SetTop(ct, 300); ct.Width = 100; ct.Height = 100;

Upvotes: 1

Related Questions