【问题标题】:How to apply a ResourceDictionary style to a control in the code behind file如何将 ResourceDictionary 样式应用于代码隐藏文件中的控件
【发布时间】:2020-06-25 22:27:56
【问题描述】:

我在自己的文件中有如下定义的 ResourceDictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WordSearch">

<Style x:Key="labelStyle">
        <Setter Property="Label.Foreground" Value="#8860D0"/>
    </Style>

</ResourceDictionary>

在我的代码隐藏文件中,我有一个标签列表,其中包含一些标签控件。

private List<Label> labels;

有什么方法可以将 ResourceDisctionary 中的样式应用于我的所有标签?到目前为止,我已经做到了这一点,但没有应用样式:

ResourceDictionary skin = Application.LoadComponent(new Uri("BrightTheme.xaml", UriKind.Relative)) as ResourceDictionary;
            Resources.MergedDictionaries.Add(skin);

            for (int i = 0; i < labels.Count; i++)
            {
                labels[i].Style = (Style)skin["labelStyle"];
            }

【问题讨论】:

  • 您需要在代码隐藏中进行吗?因为您可以在 UserControl 的 XAML 中更轻松地做到这一点。
  • 我想在代码隐藏中这样做的原因是因为该程序让用户有机会选择应用程序的不同主题。一旦选择了主题,我希望能够为这些标签重新着色。我还应该提到,这些标签是由程序自动生成的,不能硬编码,因为它们的数量也可以根据用户的输入/。
  • 如果最终目标是主题化,那么您的标签是由程序自动生成还是在 xaml 中硬编码都没有关系。您想要采取的方法是交换您的 ResourceDictionary 主题,看起来您正在尝试这样做,尽管我看到您只是添加而不是删除。我会尽快总结一个更好的答案。

标签: c# wpf visual-studio-2010 resourcedictionary


【解决方案1】:

正如我在回答之前的评论中所说,如果最终目标是主题化,那么您的标签是由程序自动生成还是在 xaml 中硬编码都没有关系。您想要采取的方法是交换您的 ResourceDictionary 主题,看起来您正在尝试这样做,尽管我看到您只是添加而不是删除。

另外,您不需要将每个标签的样式设置为键。因为要由 ResourceDictionary 来应用 Label 的整体样式。

这是一个例子:

Dictionary1.xaml:这是一个主题。请注意,第二种样式是将 labelStyle 应用于所有标签的样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="labelStyle" TargetType="Label">
        <Setter Property="Foreground" Value="#8860D0"/>
    </Style>
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource labelStyle}"/>
</ResourceDictionary>

Dictionary2.xaml:这是另一个主题

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ApplyLabelStyleOnUserControl">
    <Style x:Key="labelStyle" TargetType="Label">
        <Setter Property="Foreground" Value="DarkGreen"/>
    </Style>
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource labelStyle}"/>
</ResourceDictionary>

MainWindow.xaml

    <Grid>
        <StackPanel>
            <Button Content="Swap Theme" Click="Button_Click" Margin="15"/>
            <local:UserControl1 x:Name="userControl1" Margin="5"/>
        </StackPanel>
    </Grid

MainWindow.xaml.cs

        private bool useSecondTheme;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            useSecondTheme = !useSecondTheme;
            userControl1.SwapTheme(useSecondTheme);
        }

UserControl1.xaml

    <Grid>
        <StackPanel>
            <ListBox ItemsSource="{Binding MyLabels}">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <Border>
                                        <Label Margin="5" Content="{Binding}"/>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
            <Button Click="Button_Click" Content="Add Random Label" Margin="5"/>
        </StackPanel>
    </Grid>

UserControl1.xaml.cs:具体看SwapTheme

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ApplyLabelStyleOnUserControl
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl, INotifyPropertyChanged
    {
        private ObservableCollection<string> myLabels;

        public ObservableCollection<string> MyLabels
        {
            get
            {
                if (this.myLabels == null)
                    this.myLabels = new ObservableCollection<string>();
                return this.myLabels;
            }
        }
        private static Random random = new Random();
        public string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

        public UserControl1()
        {
            InitializeComponent();
            this.DataContext = this;
            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Initialize it with 5 random strings, but allow more labels to be added with a button on the UI.
            MyLabels.Add(RandomString(25));
            MyLabels.Add(RandomString(25));
            MyLabels.Add(RandomString(25));
            MyLabels.Add(RandomString(25));
            MyLabels.Add(RandomString(25));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Swaps between two themes
        /// </summary>
        /// <param name="useSecondTheme">when true it uses Dictionary2.xaml resource dictionary, otherwise it uses Dictionary1.xaml</param>
        internal void SwapTheme(bool useSecondTheme)
        {
            if (useSecondTheme)
            {
                // Remove the old theme
                ResourceDictionary oldSkin = Application.LoadComponent(new Uri("Dictionary1.xaml", UriKind.Relative)) as ResourceDictionary;
                Resources.MergedDictionaries.Remove(oldSkin);

                // Add the new theme
                ResourceDictionary newSkin = Application.LoadComponent(new Uri("Dictionary2.xaml", UriKind.Relative)) as ResourceDictionary;
                Resources.MergedDictionaries.Add(newSkin);
            }
            else
            {
                // Remove the old theme
                ResourceDictionary oldSkin = Application.LoadComponent(new Uri("Dictionary2.xaml", UriKind.Relative)) as ResourceDictionary;
                Resources.MergedDictionaries.Remove(oldSkin);

                // Add the new theme
                ResourceDictionary newSkin = Application.LoadComponent(new Uri("Dictionary1.xaml", UriKind.Relative)) as ResourceDictionary;
                Resources.MergedDictionaries.Add(newSkin);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MyLabels.Add(RandomString(25));
        }
    }
}

每当我点击“交换主题”按钮时,它都会在我定义的两个 ResourceDictionaries 之间切换:

【讨论】:

    【解决方案2】:

    尝试创建一个实际的ResourceDictionary 并设置它的Source 而不是调用Application.LoadComponent

    ResourceDictionary skin = new ResourceDictionary() 
    { 
        Source = new Uri("BrightTheme.xaml", UriKind.Relative) 
    };
    
    for (int i = 0; i < labels.Count; i++)
    {
        labels[i].Style = (Style) skin["labelStyle"];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2017-04-15
      相关资源
      最近更新 更多