【发布时间】:2020-09-11 18:59:31
【问题描述】:
我正在尝试通过设置DynamicResource 来动态更改GeometryDrawing 内的GeometryDrawing 的Brush。
<DrawingBrush x:Key="Vector.Close" Stretch="Uniform" AlignmentX="Center" AlignmentY="Center">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="{DynamicResource Element.Glyph.Strong}" Geometry="F1 M 38.199,40 L 0,1.801 L 1.801,0 L 40,38.199 L 38.199,40 Z"/>
<GeometryDrawing Brush="{DynamicResource Element.Glyph.Strong}" Geometry="F1 M 1.801,40 L 0,38.199 L 38.199,0 L 40,1.801 L 1.801,40 Z"/>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
当我更改应用程序的配色方案时,我将ResourceDictionary 的顺序更改为Remove() 和Insert() 在MergedDictionary 中:
public static void SelectTheme(string id = "Light")
{
var theme = Application.Current.Resources.MergedDictionaries.FirstOrDefault(f => f.Source != null && f.Source.ToString().EndsWith($"Colors/{id}.xaml"));
if (theme == null)
{
theme = Application.Current.Resources.MergedDictionaries.FirstOrDefault(f => f.Source != null && f.Source.ToString().EndsWith("Colors/Light.xaml"));
UserSettings.All.MainTheme = AppTheme.Light;
}
Application.Current.Resources.MergedDictionaries.Remove(theme);
Application.Current.Resources.MergedDictionaries.Add(theme);
}
比如Light.xaml里面,我有一个SolidColorBrush:
<SolidColorBrush x:Key="Element.Glyph.Strong" Color="#FF231F20"/>
在App.xaml 中,我导入了我的两个资源字典(顺序无关紧要,因为资源将移到最后一个位置):
<!--Themes-->
<ResourceDictionary Source="/Themes/Colors/Dark.xaml"/>
<ResourceDictionary Source="/Themes/Colors/Light.xaml"/>
<ResourceDictionary Source="/Resources/Vectors.xaml"/>
<ResourceDictionary Source="/Themes/Button.xaml"/>
矢量用作自定义Button 上的图标。按钮内部有一个带大小和对齐方式的边框,向量用作Background。
Button定义和用法(Icon是BrushDependencyProperty):
public class ExtendedButton : Button
{
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon), typeof(Brush), typeof(ExtendedButton));
public Brush Icon
{
get => (Brush)GetValue(IconProperty);
set => SetCurrentValue(IconProperty, value);
}
//Ctor and other stuff.
}
<n:ExtendedButton Style="{DynamicResource Style.Button.NoText}"
Icon="{DynamicResource Vector.Close}" Width="30" Padding="6"/>
我将Icon 设置为StaticResource,但我在此处询问之前更改为DynamicResource,作为测试。但是还是不行。
这是按钮的简化版Style:
<!--Button • No Border • No Text-->
<Style TargetType="{x:Type n:ExtendedButton}" BasedOn="{StaticResource {x:Type Button}}" x:Key="Style.Button.NoText">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type n:ExtendedButton}">
<Border x:Name="MainBorder" MinHeight="{TemplateBinding MinHeight}" Background="{TemplateBinding Background}">
<Grid x:Name="InnerGrid">
<Border Background="{TemplateBinding Icon}" Margin="{TemplateBinding Padding}" Opacity="{DynamicResource Element.Opacity}"
Height="14" Width="14" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource Brush.Button.Background.Hover}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource Brush.Button.Background.Pressed}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.7"/>
</Trigger>
</Style.Triggers>
</Style>
应用程序的其余部分正常工作,当更改主题时,其他所有内容都会更改。 GeometryDrawing 中的 Element.Glyph.Strong 不会更新。
只有在关闭应用并再次打开时才会以正确的颜色显示矢量,因为它会以新主题加载。
我猜想Brush 属性作为DependencyProperty 也会在资源更新时更新。
我想知道如何将Brush 设置为动态颜色,而无需使用后面的代码?
【问题讨论】:
标签: wpf resourcedictionary dynamicresource