简体   繁体   中英

Why is my BorderBrush MultiBinding returning correctly but being ignored

I have a Border that I wanted to set BorderBrush on to a MultiBinding but for some reason I only end up with no border at all everytime. It works fine with a single binding and IValueConverter. When I put a breakpoint in Convert Method in MultiValueConverter I see the correct color being set and returned. For some reason its not reflected when UI is rendered and I'm not sure why?

I thought maybe I needed to put it in a Style Trigger but I have a very similar multi-binding with a Rectangle.Fill that works fine. I also considered an overwriting style issue but I removed them all and got the same result.

<Border Grid.Row="1" Grid.Column="3" BorderThickness="3,3,3,3">
            <Border.BorderBrush>
                <MultiBinding>
                    <MultiBinding.Converter>
                        <conv:StatusBorderColorConverter />
                    </MultiBinding.Converter>
                    <Binding Path="ExternalAlarms.HighestStatus" IsAsync="True" />
                    <Binding Path="InternalAlarms.HighestStatus" IsAsync="True" />
                </MultiBinding>
            </Border.BorderBrush> 

public class StatusBorderColorConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values != null && values.Length == 2 && values[0] != DependencyProperty.UnsetValue && values[1] != DependencyProperty.UnsetValue)
        {
            int status1 = System.Convert.ToInt32(values[0]);
            int status2 = System.Convert.ToInt32(values[1]);
            int[] statuses = new int[] { status1, status2 };
            int status = statuses.Max();

            Color c = new Color();

            if (status < 0 || status > 12)
                c = Color.FromRgb(255, 255, 255);
            else if (status == 0)
                c = Color.FromRgb(0, 0, 0);
            else if (status < 5)
                c = Color.FromRgb(255, 255, 80);
            else if (status < 9)
                c = Color.FromRgb(255, 153, 0);
            else if (status < 13)
            {
                c = Color.FromRgb(255, 38, 0);
            }

            return c;
        }
        return Binding.DoNothing;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    } 
}

In your converter instead of

return c;

do

return new SolidColorBrush(c);

or change binding to be

<Border.BorderBrush>
    <SolidColorBrush>
        <SolidColorBrush.Color>
            <MultiBinding>
                <MultiBinding.Converter>
                    <conv:StatusBorderColorConverter />
                </MultiBinding.Converter>
                <Binding Path="ExternalAlarms.HighestStatus" IsAsync="True" />
                <Binding Path="InternalAlarms.HighestStatus" IsAsync="True" />
            </MultiBinding>
        </SolidColorBrush.Color>
    </SolidColorBrush>
</Border.BorderBrush> 

Problem is that your converter returns Color and BorderBrush is a Brush

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM