允许用户修改资源字典

本文关键字:修改 资源 字典 用户 许用户 | 更新日期: 2023-09-27 18:11:21

我所有的样式和模板都位于一个名为"design "的资源字典中。所以在每个Usercontrol中我都有:

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MyProject;component/design.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

但是我想让用户通过创建2个资源字典来选择他喜欢的设计,其中包含相同的模板(每个模板具有不同颜色的相同键)。

例如文件设计。Xaml和design2.xaml

我该怎么做呢?是否可以使用代码动态更改资源字典?

允许用户修改资源字典

ResourceDictionary1

<Style x:Key="StyleTitleText" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Arial" />
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Foreground" Value="Green" />
</Style>

ResourceDictionary2

<Style x:Key="StyleTitleText" TargetType="TextBlock">
  <Setter Property="FontFamily" Value="Arial" />
  <Setter Property="FontSize" Value="14"/>
  <Setter Property="Foreground" Value="Red" />
</Style>

主窗口xaml

如果主题需要动态更新UI(即不重载整个UI),则需要使用DynamicResource而不是StaticResource。

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="design.xaml" />
            <ResourceDictionary Source="design2.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<StackPanel Orientation="Horizontal">
    <ToggleButton Name="Radiobtn" Content="Switch ResourceDictionary" Height="35"   FontSize="12" Margin="0,0,50,0"  Click="RadioButton_Checked_1"></ToggleButton>
    <TextBlock Style="{DynamicResource StyleTitleText}" Text="hfghfhgfhgfhgfghfhfhgf" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</StackPanel>
c#

  ResourceDictionary r1;
  ResourceDictionary r0;
    public MainWindow()
    {
        InitializeComponent();
        r1 = this.Resources.MergedDictionaries[1];
        r0 = this.Resources.MergedDictionaries[0];
        this.Resources.MergedDictionaries.Remove(r1);
    }
    private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
    {
        this.Resources.Clear();
        if (Radiobtn.IsChecked == true)
        {
            this.Resources.MergedDictionaries.Add(r0);
        }
        else
        {
            this.Resources.MergedDictionaries.Add(r1);
        }
    }

您可以使用下面的代码轻松地在代码中加载ResourceDictionary:

ResourceDictionary resourceDictionary = new ResourceDictionary { Source = 
    new Uri("/AssemblyName;component/OptionalFolderName/ResourceDictionaryName.xaml", 
    UriKind.RelativeOrAbsolute) };
YourCurrentUserControlOrWindow.Resources.MergedDictionaries.Add(resourceDictionary);

假设您有一个允许用户选择样式的组合框下拉框,您可以做以下操作

ViewModel,带有通过名为StyleDefinition的数据模型定义的样式集合

// Showing the viewmodel constructor only
public ViewModel()
{
    this.AvailableStyles = new ObservableCollection<StyleDefinition>()
    {
        new StyleDefinition() { Name="Red", ResourceUri="Design.xaml" },
        new StyleDefinition() { Name="Green", ResourceUri="Design2.xaml" }
    };
}
// Definition of a simple data model to store style name and uri
public class StyleDefinition
    {
        public string Name { get; set; }
        public string ResourceUri { get; set; }
    }

现在将集合绑定到组合框,并在selectionchanged

上设置一个简单的事件处理程序
<ComboBox Height="50" x:Name="StyleChanger" ItemsSource="{Binding Path=AvailableStyles}" 
              SelectedValuePath="ResourceUri" DisplayMemberPath="Name" 
              SelectionChanged="StyleChanger_SelectionChanged">

选择更改后的代码:

private void StyleChanger_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var style = this.StyleChanger.SelectedItem as StyleDefinition;
    this.Resources.MergedDictionaries[0].Source = new Uri(style.ResourceUri, UriKind.Relative);
}

底线是,您只需要将MergedDictionaries[0]的Source属性更改为用户选择的那个。这将动态地改变样式