WPF 主题和转换器

本文关键字:转换器 WPF | 更新日期: 2023-09-27 18:31:27

我有一个支持多种配色方案的 WPF 应用程序(我通过在运行时交换 Black.Xaml 和 White.XAML 文件来实现这一点,其中两者都具有具有相同键的画笔,然后使用 DynamicResources 访问它们)。

现在,我必须为低于 0 的值显示红色,为高于 0 的值显示绿色。(我为每个主题有不同的绿色和红色)。所以我的转换器是这样的

XAML

<Converters:PositiveNegativeConverter x:Key="PositiveNegativeConverter" DownColor="{DynamicResource RedPrimaryBrush}" UpColor="{DynamicResource GreenPrimaryBrush}" NormalColor="{DynamicResource BluePrimaryBrush}"/>

转炉

 public class PositiveNegativeConverter : DependencyObject, IValueConverter
{
    #region DependencyProperty
    public SolidColorBrush UpColor
    {
        get { return (SolidColorBrush)GetValue(UpColorProperty); }
        set { SetValue(UpColorProperty, value); }
    }
    // Using a DependencyProperty as the backing store for UpColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UpColorProperty =
        DependencyProperty.Register("UpColor", typeof(SolidColorBrush), typeof(PositiveNegativeConverter), new PropertyMetadata(null));

    public SolidColorBrush DownColor
    {
        get { return (SolidColorBrush)GetValue(DownColorProperty); }
        set { SetValue(DownColorProperty, value); }
    }
    // Using a DependencyProperty as the backing store for DownColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DownColorProperty =
        DependencyProperty.Register("DownColor", typeof(SolidColorBrush), typeof(PositiveNegativeConverter), new PropertyMetadata(null));

    public SolidColorBrush NormalColor
    {
        get { return (SolidColorBrush)GetValue(NormalColorProperty); }
        set { SetValue(NormalColorProperty, value); }
    }
    // Using a DependencyProperty as the backing store for NormalColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NormalColorProperty =
        DependencyProperty.Register("NormalColor", typeof(SolidColorBrush), typeof(PositiveNegativeConverter), new PropertyMetadata(null));
    #endregion
    #region IValueConverter
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is double)
        {
            double dblValue = (double)value;
            if (dblValue > 0)
            {
                return UpColor;
            }
            else if (dblValue < 0)
            {
                return DownColor;
            }
        }
        return NormalColor;
    }
}

但是当我在运行时更改主题时,我没有得到转换器的点击,甚至我在转换器中的画笔属性也没有被更改(例如:白色特定的绿色没有更改为黑色特定的绿色),那么我如何才能获得可接受的结果。

没有转换器的所有其他方案都运行良好。

WPF 主题和转换器

您是否尝试过不使用 DependencyObject 与 IValueConverter 结合使用?