【问题标题】:Foreground issue when using with Dynamic resource与动态资源一起使用时的前台问题
【发布时间】:2013-11-08 13:22:26
【问题描述】:

对于我的自定义控件,我使用动态资源来设置前景属性。最初,当我运行应用程序时,Foreground 属性未设置为我的控件。当我动态更改值时,前景被正确应用。我该如何解决这个问题?

PS:简单示例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>

【问题讨论】:

  • 除了ButtonBase_OnClick 处理程序外,您没有在任何地方应用您的Style,所以也许是为什么直到您单击Button 才应用它?跨度>

标签: wpf xaml styles foreground dynamicresource


【解决方案1】:

如果要将Style 应用于控件,则必须将Style 应用于该控件(您在提供的代码示例中没有这样做)。你有两个选择......要么试试这个(无论你在哪里使用控件):

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

或者像这样定义您的Style,这将影响范围内您的控件的所有个实例:

<Style TargetType="ContentControl">
    <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
    <Setter Property="Background" Value="{DynamicResource BackgroundBrush}" />
</Style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多