-1

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();
        }
Hhry
  • 823
  • 1
  • 8
  • 19

1 Answers1

1

How about using triggers instead

<Border.Style>
    <Style TargetType="Border">
        <Setter Property="Background" Value="{DynamicResource ProfileBorderBackground}"/>
        <Style.Triggers>
           <DataTrigger Binding="{Binding IsSelected}" Value="True">
               <Setter Property="Background" Value="{DynamicResource ProfileBorderSelectedBackground}"/>
           </DataTrigger>
        </Style.Triggers>
   </Style>
</Border.Style>

If you don't use DynamicResource

    <Border CornerRadius="8" Width="214" Height="112">
        <Border.Background>
            <!--your Converter-->
            <MultiBinding Converter="{StaticResource LatencyToColorConverter}">
                <MultiBinding.Bindings>
                    <Binding Path="latency"/>
                    <Binding Path="IsSelected"/>
                </MultiBinding.Bindings>
            </MultiBinding>
        </Border.Background>
    </Border>

Triggers Version

<Border.Style>
    <Style TargetType="Border">
        <!--Default Background-->
        <Setter Property="Background" Value="#000000"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding latency,Converter={StaticResource cvtLatencyBackground}}" Value="0">
                <!--Value="{DynamicResource XXXX}"-->
                <Setter Property="Background" Value="#FFFFFF"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding latency,Converter={StaticResource cvtLatencyBackground}}" Value="1">
                <Setter Property="Background" Value="#C30D23"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding latency,Converter={StaticResource cvtLatencyBackground}}" Value="2">
                <Setter Property="Background" Value="#11B29F"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding latency,Converter={StaticResource cvtLatencyBackground}}" Value="3">
                <Setter Property="Background" Value="#F29600"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                <Setter Property="Background" Value="#FFFFFF"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Border.Style>

The Converter

public class LatencyBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || !int.TryParse(value.ToString(), out int latency)) return -1;
        switch (latency)
        {
            case 0:
            case -1:
                return 0;
            case -2:
                return 1;
            default:
                if (latency < 1000)
                {
                    return 2;
                }
                else
                {
                    return 3;
                }
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Pai
  • 21
  • 4
  • This works like a charm! Thanks for suggesting the alternative method :). However the ``Binding="IsSelected"`` need to be ``Binding="{Binding IsSelected}"`` I think? – Hhry Apr 08 '21 at 06:55
  • However, if I want to convert a double variable, this might not work. – Hhry Apr 08 '21 at 07:42
  • Can use MultiBinding or MultiDataTrigger – Pai Apr 08 '21 at 09:01