我有一个支持多种配色方案的WPF应用程序(我通过在运行时交换Black.Xaml和White.XAML文件来实现此目的,其中两个都使用同一个关键点,然后使用DynamicResources访问它们)。WPF主题和转换器
现在我必须表明红的值低于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;
}
}
但是当我改变在运行时的主题,我没有得到被打到的转换器,甚至我的画笔在转换器中的属性会发生变化(例如:白色特定绿色没有变成黑色特定绿色),那么我怎样才能达到可接受的结果。
没有转换器的所有其他场景运行良好。
您可以提供一个专用的类'ThemeManager',当您更改当前主题时,您可以让事件发生。然后,视图可以订阅它(订阅可以移动到附加的行为,如果你喜欢MVVM的概念)和更新绑定('PropertyChanged(“”)'或重新分配'DataContext')。没有其他办法,你必须触发“PropertyChanged”事件或自己处理绑定。转换器仅在此之后被调用。 – Sinatr