Doomsknight Doomsknight - 1 month ago 23
C# Question

How do you assign the style programmatically

I am trying to set one of my xaml styles to my frame in my page.
It is created in code, and assigned to the layout dynamically.

So I expect that I have to set the style dynamically? since the frame does not exist in xaml.

What I cannot figure out, is how to assign a custom template. Or better yet, in a global manner to any frames that fit a certain category. Tag, or type etc.

Below is my template that im trying to test with. But it does not work. Assume code is missing, so started checking codebehind style setting, but no luck so far.

App.xaml

<!-- http://paulstovell.com/blog/wpf-navigation -->
<ControlTemplate TargetType="Frame" x:Key="frame" >
<DockPanel Margin="7">
<StackPanel
Margin="7"
Orientation="Horizontal"
DockPanel.Dock="Top"
>
<Button
Content="Avast! Go back!"
Command="{x:Static NavigationCommands.BrowseBack}"
IsEnabled="{TemplateBinding CanGoBack}"
/>
<Button
Content="Forward you dogs!"
Command="{x:Static NavigationCommands.BrowseForward}"
IsEnabled="{TemplateBinding CanGoForward}"
/>
</StackPanel>

<Border
BorderBrush="Green"
Margin="7"
BorderThickness="7"
Padding="7"
CornerRadius="7"
Background="White"
>
<ContentPresenter />
</Border>
</DockPanel>
</ControlTemplate>


MyWindow.xaml.cs

Frame newFrame = new Frame();
newFrame.Content = content;

newFrame.Template = ControlTemplate ...?

Answer

Option 1:

  1. Create a style without a Key (inplicit) for your type
  2. In the style add the ControlTemplate
  3. When you add the control (even it from code) it will get the default style you just created

Code Ex with a button that get hist style form the containing window:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Red"></Setter>
            <Setter Property="Template">
               <ControlTemplate>
                   <... Your Template ...>
               </ControlTemplate>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>

    </Grid>
</Window>

Create the Button from Code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var button = new Button();
        this.Content = button;
    }
}

Option 2:

  1. Create a style with a key.
  2. In the style add your ControlTemplate
  3. Add the style to the App resources.
  4. Get The style from Application resouces and set the style (and the template):

Code Ex:

var yourStyle = (Style)Application.Current.Resources["Resource_Name"]);

Frame newFrame = new Frame();

newFrame.Style = yourStyle;