【问题标题】:Changing XAML style dynamically in Code Behind so that controls applying that style also reflect the change在代码隐藏中动态更改 XAML 样式,以便应用该样式的控件也反映更改
【发布时间】:2012-05-07 21:05:54
【问题描述】:

我希望能够在我的 WPF 窗口中从 .cs 文件设置样式属性(和值)。

我的问题是,如果我有 30 个矩形,我希望所有这些矩形都具有相同的样式(并且我不想单独更新所有这些矩形)。我想让它们(在 xaml 文件中)全部设置为相同的样式,然后更新样式以使其看起来像我想要的那样。

假设我在 Xaml 中为每个矩形设置了 Style = "key1"。然后我希望以后能够修改“key1”,以便所有矩形都反映该更改。

我试过App.xaml

<Application.Resources>
    <Style x:Key="key1" TargetType="Rectangle">
        <Setter Property="Fill" Value="Red"/>
    </Style>
</Application.Resources>

在 MainwWindows.xaml 中

<StackPanel>
    <Rectangle Style="{StaticResource key1}" Height="200" Width="200" x:Name="rect1"/>
    <Button Click="Button_Click" Content="Click"/>
</StackPanel>

在后面的代码中

private void Button_Click(object sender, RoutedEventArgs e)
{
    Style style = Application.Current.Resources["key1"] as Style;
    style.Setters.Add(new Setter(Rectangle.VisibilityProperty, Visibility.Collapsed));
}

这会更新样式,但不会更新矩形。

这可能吗?有谁知道如何做到这一点? (一个例子将不胜感激)。

【问题讨论】:

  • 我认为您必须在 UI 元素上调用 Update()Refresh() 方法(仅限整个窗口或矩形)。试试看,可能会有帮助。

标签: c# wpf styles


【解决方案1】:

您需要使用DynamicResource 以便在运行时更改它。您还需要用新样式替换样式,而不是尝试修改现有样式。这有效:

<StackPanel>
    <Rectangle Style="{DynamicResource key1}" Height="200" Width="200" x:Name="rect1"/>
    <Button Click="Button_Click" Content="Click"/>
</StackPanel>

Style style = new Style {TargetType = typeof(Rectangle)};
style.Setters.Add(new Setter(Shape.FillProperty, Brushes.Red));
style.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));

Application.Current.Resources["key1"] = style;

【讨论】:

  • 可以只在 xaml 端做吗?
【解决方案2】:

还值得一提的是,样式一旦使用就被密封,因此无法更改。这就是为什么样式应该被另一个实例替换而不是更新的原因。

【讨论】:

    【解决方案3】:

    创建了一些静态助手,用法示例:

    SetStyle(typeof(ContentPage), 
       (ContentPage.BackgroundColorProperty, Color.Green), 
       (ContentPage.PaddingProperty, new Thickness(20)));
    

    辅助方法:

        public static Style CreateStyle(Type target, params (BindableProperty property, object value)[] setters)
        {
            Style style = new Style(target);
            style.ApplyToDerivedTypes = true;
            foreach (var setter in setters)
            {
                style.Setters.Add(new Setter
                {
                    Property = setter.property,
                    Value = setter.value
                });
            }
            return style;
        }
    
        public static void SetStyle(Type target, params (BindableProperty property, object value)[] setters)
        {
            Style style = new Style(target);
            style.ApplyToDerivedTypes = true;
            foreach (var setter in setters)
            {
                style.Setters.Add(new Setter
                {
                    Property = setter.property,
                    Value = setter.value
                });
            }
            Application.Current.Resources.Add(style);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 2016-12-06
      • 2011-10-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2018-04-04
      • 2014-12-19
      相关资源
      最近更新 更多