为什么我的BorderBrush MultiBinding正确返回但被忽略

时间:2014-07-15 19:45:27

标签: c# wpf xaml data-binding

我有一个Border,我想将BorderBrush设置为MultiBinding,但由于某种原因,我每次都只会没有边框。单个绑定和IValueConverter可以正常工作。当我在MultiValueConverter中的Convert Method中放置一个断点时,我看到正确的颜色被设置并返回。出于某些原因,在呈现UI时没有反映出来,我不确定为什么?

我想也许我需要将它放在样式触发器中,但我有一个非常类似的多重绑定与Rectangle.Fill工作正常。我也考虑过覆盖样式问题,但我将它们全部删除并得到了相同的结果。

<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;
    } 
}

1 个答案:

答案 0 :(得分:2)

在您的转换器中而不是

return c;

return new SolidColorBrush(c);

或将绑定更改为

<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> 

问题是您的转换器返回ColorBorderBrushBrush