Question

For my custom control, I am using the Dynamic resource to set the Foreground property. Initially when i run the app the Foreground property was not set to my control. When I change value dynamically Foreground was applied correctly. How can I resolve this issue?

PS: Simple sample WpfApplication4

MainWindow.xaml

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Width="300"
    Height="200">
<Window.Resources>
    <SolidColorBrush x:Key="ForegroundBrush" Color="Red" />
    <SolidColorBrush x:Key="BackgroundBrush" Color="Yellow" />

    <Style x:Key="MyStyle" TargetType="ContentControl">
        <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
        <Setter Property="Background" Value="{DynamicResource BackgroundBrush}" />
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel>
        <Button Click="ButtonBase_OnClick" Content="Create and Add Custom Control With Style" />
        <Button Click="ButtonBase_OnClick_1" Content="Change Color" />
    </StackPanel>
    <StackPanel x:Name="myPanel" Grid.Row="1" />
</Grid>

MainWindow.xaml.cs

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

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var control = new MyControl {Style = this.Resources["MyStyle"] as Style};
        control.CreateContent(string.Format("My Control {0}", myPanel.Children.Count + 1));
        myPanel.Children.Add(control);
    }

    private void ButtonBase_OnClick_1(object sender, RoutedEventArgs e)
    {
        var brush = (SolidColorBrush)Resources["ForegroundBrush"];
        Resources["ForegroundBrush"] = new SolidColorBrush(Color.Add(brush.Color, Color.FromRgb(0, 100, 100)));

        var brush1 = (SolidColorBrush)Resources["BackgroundBrush"];
        Resources["BackgroundBrush"] = new SolidColorBrush(Color.Add(brush1.Color, Color.FromRgb(0, 100, 100)));
    }
}

public class MyControl : ContentControl
{
    public MyControl()
    {
        DefaultStyleKey = typeof(MyControl);
    }

    public void CreateContent(string text)
    {
        this.Content = new TextBlock() {Text = text};
    }
}

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfApplication4">
<Style TargetType="local:MyControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MyControl">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter Content="{TemplateBinding Content}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Was it helpful?

Solution

If you want to apply a Style to a control, then you have to apply the Style to that control (which you haven't done in the code example that you have provided). You have two choices here... either try this (wherever you use the control):

<local:MyControl Style="{StaticResource MyStyle}" />

Or define your Style like this, which will affect all instances of your control within scope:

<Style TargetType="ContentControl">
    <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
    <Setter Property="Background" Value="{DynamicResource BackgroundBrush}" />
</Style>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top