Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF主题和转换器_C#_.net_Wpf_Xaml - Fatal编程技术网

C# WPF主题和转换器

C# WPF主题和转换器,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我有一个支持多种颜色方案的WPF应用程序(我通过在运行时交换Black.Xaml和White.Xaml文件来实现这一点,在这两个文件中都有具有相同密钥的画笔,然后使用DynamicResources访问它们) 现在,对于低于0的值,我必须显示红色,对于高于0的值,我必须显示绿色。(每个主题我有不同的绿色和红色)。我的转换器是这样的 XAML 转换器 public class PositiveNegativeConverter : DependencyObject, IValueConver

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

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

XAML


转换器

 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;
    }
}
公共类PositiveEnegativeConverter:DependencyObject,IValueConverter
{
#区域依赖性
公共SolidColorBrush UpColor
{
获取{return(SolidColorBrush)GetValue(UpColorProperty);}
set{SetValue(UpColorProperty,value);}
}
//使用DependencyProperty作为UpColor的后台存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty UpColorProperty=
DependencyProperty.Register(“UpColor”、typeof(SolidColorBrush)、typeof(PositiveNegativeConverter)、new PropertyMetadata(null));
公共SolidColorBrush DownColor
{
get{return(SolidColorBrush)GetValue(DownColorProperty);}
set{SetValue(DownColorProperty,value);}
}
//使用DependencyProperty作为DownColor的后台存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty DownColorProperty=
DependencyProperty.Register(“DownColor”、typeof(SolidColorBrush)、typeof(PositiveNegativeConverter)、new PropertyMetadata(null));
公共SolidColorBrush NormalColor
{
获取{return(SolidColorBrush)GetValue(NormalColorProperty);}
set{SetValue(NormalColorProperty,value);}
}
//使用DependencyProperty作为NormalColor的后台存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty NormalColorProperty=
DependencyProperty.Register(“NormalColor”、typeof(SolidColorBrush)、typeof(PositiveNegativeConverter)、new PropertyMetadata(null));
#端区
#区域变换器
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
如果(值为双精度)
{
double dblValue=(double)值;
如果(dblValue>0)
{
返回上色;
}
else if(dblValue<0)
{
返回底色;
}
}
返回正常颜色;
}
}
但是,当我在运行时更改主题时,我不会让转换器命中,甚至不会更改转换器中的笔刷属性(例如:特定于白色的绿色不会更改为特定于黑色的绿色),因此如何才能获得可接受的结果


没有转换器的所有其他场景都运行良好。

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

您可以提供一个专用类
TheeManager
,当当前主题更改时,您可以在该类中引发事件。然后view可以订阅它(如果您喜欢MVVM概念,可以将订阅移动到附加的行为)并更新绑定(
PropertyChanged(“”)
或重新分配
DataContext
)。没有其他方法,您必须触发“PropertyChanged”事件或自己处理绑定。转换器仅在此之后调用。
 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;
    }
}