【发布时间】:2021-04-08 04:09:32
【问题描述】:
我在控件的后台使用自定义转换器。
<Border Background="{Binding IsSelected, Converter={StaticResource SelectedToProfileBorderBackgroundConverter}}" CornerRadius="8" Width="214" Height="112">
我的转换器将从我声明的资源字典中返回一个画笔。
public class SelectedToProfileBorderBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool status = (bool)value;
if (status)
{
return Application.Current.Resources["ProfileBorderSelectedBackground"];
}
else
{
return Application.Current.Resources["ProfileBorderBackground"];
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<SolidColorBrush x:Key="ProfileBorderBackground" Color="#EEEEEF"/>
<SolidColorBrush x:Key="ProfileBorderSelectedBackground" Color="#EEEEEF"/>
问题是Application.Current.Resources["ProfileBorderSelectedBackground"] 返回一个静态资源而不是动态资源。
因此,如果我通过更改用户选择的主题来更改 ProfileBorderSelectedBackground 的值,则边框背景不会更改。
有没有办法让Application.Current.Resources["ProfileBorderSelectedBackground"] 返回一个 DynamicResource 而不是 StaticResource?
更新: 我需要在 Converter 中使用 switch 语句怎么样?
public class LatencyToColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool status = (bool)values[1];
if (status)
{
return new SolidColorBrush(Color.FromRgb(255, 255, 255));
}
else
{
long latency = (long)values[0];
switch (latency)
{
case 0:
case -1:
return new SolidColorBrush(Color.FromRgb(195, 13, 35));
case -2:
return new SolidColorBrush(Color.FromRgb(158, 158, 159));
default:
if (latency < 1000)
{
return new SolidColorBrush(Color.FromRgb(17, 178, 159));
}
else
{
return new SolidColorBrush(Color.FromRgb(242, 150, 0));
}
}
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
【问题讨论】:
-
为什么在编辑中使用
IMultiValueConverter?
标签: c# wpf xaml ivalueconverter