I'm using my custom converter in my control's background.
<Border Background="{Binding IsSelected, Converter={StaticResource SelectedToProfileBorderBackgroundConverter}}" CornerRadius="8" Width="214" Height="112">
My converter will return a brush from the resourcedictionary that I declared.
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"/>
The problem is Application.Current.Resources["ProfileBorderSelectedBackground"]
return a staticResource instead of DynamicResource.
So if I change the value of ProfileBorderSelectedBackground
through a change in theme that user selected, the border background is not changed.
Is there a way to let Application.Current.Resources["ProfileBorderSelectedBackground"]
return a DynamicResource instead of StaticResource?
Updated: How about I need to use switch statement in Converter?
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();
}