0

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

adminSoftDK
  • 2,012
  • 1
  • 21
  • 41
  • Why would you want to replace perfectly usable XAML with suboptimal C#? – Mike Eason Sep 18 '15 at 14:20
  • @MikeEason I am implementing some custom behaviours and custom controls, and this is just a code snipped of a much bigger control. – adminSoftDK Sep 18 '15 at 14:33
  • @adminSoft. Do you really want to change the Template of control with C#, or is ok for you to change Content property to provide custom Content to your UserControl with C#? – Emmanuel DURIN Sep 18 '15 at 15:49

1 Answers1

1

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.

Community
  • 1
  • 1
Dazzibao
  • 590
  • 3
  • 7