让用户更改资源字典

时间:2014-07-03 12:02:37

标签: c# wpf xaml resources resourcedictionary

我的所有样式和模板都位于名为“design.xaml”的资源字典中,所以在每个Usercontrol中我都有:

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

但是我想让用户通过在内部创建具有相同模板的2个资源字典来选择他的首选设计(每个模板使用不同颜色的相同键)。

例如文件design.xaml和design2.xaml

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

谢谢!

3 个答案:

答案 0 :(得分:1)

<强> 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>

MainWindow xaml

如果主题需要动态更新UI(即无需重新加载整个UI),则需要在StaticResource上使用DynamicResource。

<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);
        }
    }

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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属性更改为用户选择的属性。这会动态地改变风格。