adminSoftDK
adminSoftDK

Reputation: 2092

How to translate wpf xaml controltemplate and its content to c#

I’ve got this piece of XAML in WPF, and I’d like to translate it to C# but I cannot find any way of doing it.

<Style x:Key="OptionsStyle" TargetType="{x:Type UserControl}">
    <Style.Resources>
                <ResourceDictionary Source="/Resources;component/BorderStyle.xaml" />
    </Style.Resources>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type UserControl}">
                <ContentPresenter Content="{StaticResource FormBorderStyle}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Here is what I tried

public class MyUserControl : UserControl
{
    public MyUserControl()
    {
        Template = new ControlTemplate(typeof(UserControl))
        {
            SOMETHING (there is not big choice here :( ) = new ContentPresenter() { Content = FindResource("FormBorderStyle") }
        };
    }
}

I just can’t find a way to put that ContentPresenter into that ControlTemplate, or maybe it should be something else instead of ControlTemplate?

Kind Regards

Upvotes: 0

Views: 254

Answers (1)

Dazzibao
Dazzibao

Reputation: 610

You should use the VisualTree property of the ControlTemplate, which if of type FrameworkElementFactory. Alternatively, you can use XamlReader if you can keep the content of your ControlTemplate in Xaml. See this question on stackoverflow.

Upvotes: 1

Related Questions