【问题标题】:Changing the styles at runtime in WPF在 WPF 中运行时更改样式
【发布时间】:2013-08-13 04:49:11
【问题描述】:

我试图允许用户自定义 WPF 应用程序中的元素。我想要实现的是,如果我有一个指定所有表单元素(文本框、标签等)的列表框,用户可以选择一个表单元素,并将样式属性设置为标签,前景应该是橙色的TextBox 前景应该是黑色的等等。根据我打算应用的任何样式,所有文本框都应该看起来相似。

我无法找到实现这一目标的方法。我已经尝试了一个可以在运行时上传多个预定义样式的示例。所以现在,我想找到一种在运行时改变不同元素属性的方法。

UPDATE:

我试图从后面的代码中创建一个新样式。

XAML

<Label Content="SAMPLE" Style="{DynamicResource Style1}" x:Name="label1" />
<Button Content="Button" Click="Button_Click" />

在后面的代码中,即单击按钮时,我尝试了这个:

Style style = new Style { TargetType = typeof(Label) };
style.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Black));
Application.Current.Resources["Style1"] = style;

但它没有得到更新。

谢谢。

【问题讨论】:

  • 来自 MSDN:一旦应用了样式,它就会被密封并且无法更改。如果要动态更改已应用的样式,则必须创建新样式来替换现有样式。
  • 你认为这行 Application.Current.Resources["Style1"] = style; 将取代你在 XAML 中写的 Style 吗?
  • 这是我从stackoverflow.com/questions/10345166/…尝试的解决方案

标签: c# wpf styles


【解决方案1】:

您必须确保样式在文件App.xaml中:

<Application x:Class="ChangeStyleHelp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">

    <Application.Resources>
        <Style x:Key="MyStyle" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="Green" />
        </Style>
    </Application.Resources>
</Application>

后面的代码:

private void ChangeStyle_Click(object sender, RoutedEventArgs e)
{
    Style style = new Style 
    { 
        TargetType = typeof(Label) 
    };

    style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));

    Application.Current.Resources["MyStyle"] = style;
}   

如果StyleWindowWindow.Resources)的资源中,则需要写this,或者Window的名称:

private void ChangeStyle_Click(object sender, RoutedEventArgs e)
{
    Style style = new Style 
    { 
        TargetType = typeof(Label) 
    };

    style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));

    this.Resources["MyStyle"] = style;
}   

同样,您可以通过这种方式更改Style:采用现有样式并使用元素。示例:

<Application x:Class="ChangeStyleHelp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">

    <Application.Resources>
        <Style x:Key="AnotherWayStyle" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="Lavender" />
            <Setter Property="Foreground" Value="OrangeRed" />
        </Style>
    </Application.Resources>
</Application>  

后面的代码:

private void AnotherWay_Click(object sender, RoutedEventArgs e)
{
    label1.Style = (Style)Application.Current.Resources["AnotherWayStyle"];
}   

【讨论】:

    【解决方案2】:

    您是否尝试过使用资源字典

    资源字典

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="TextColor" Color="#FF121212"/>
    </ResourceDictionary>
    

    控件的 XAML

    <TextBox Text="TextBox" Foreground="{DynamicResource TextColor}" />
    

    在运行时更改样式的代码

         var rd = new ResourceDictionary();
         rd.Add("TextColor", "#FFFFFF");
         Application.Current.Resources.MergedDictionaries.Add(rd);
    

    这会将您的新样式与现有样式合并,并且更改将自动反映在与这些样式链接的所有控件上。

    【讨论】:

      【解决方案3】:

      它对我很有用:

      Xaml:

      <TreeView x:Name="TreePeople">
          <TreeView.ItemContainerStyle>
              <Style TargetType="{x:Type TreeViewItem}">
                  <Setter Property="IsExpanded" Value="True" />
              </Style>
          </TreeView.ItemContainerStyle>
      </TreeView> 
      

      c#:

      bool Expanded = false; 
      // The event subscription method (for a button click)
      private void ButtonExpand__Click(object sender, RoutedEventArgs e)
      {
          Expanded = !Expanded;
          Style Style = new Style
          {
              TargetType = typeof(TreeViewItem)
          };
      
          Style.Setters.Add(new Setter(TreeViewItem.IsExpandedProperty, Expanded));
          TreePeople.ItemContainerStyle = Style;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-10-09
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 2020-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-09
        相关资源
        最近更新 更多