且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF Datagrid-与其他列相关的列绑定

更新时间:2022-03-01 22:51:58

多绑定就是这样!

这是我的解决方法:

<Setter Property="Foreground">
    <Setter.Value>
        <MultiBinding Converter="{StaticResource frgConverter}">
            <Binding Path="state"/>

            <Binding Path="color1"/>
            <Binding Path="color2"/>
            <Binding Path="color3"/>
        </MultiBinding>
    </Setter.Value>
</Setter>

和转换器:

public class GridForegroundConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int iColor;
        int nState = System.Convert.ToInt32(values[0]);

        if (nState < 0 || nState > 3)
            throw new ArgumentOutOfRangeException("State");

        iColor = System.Convert.ToInt32(values[nState + 1]);

        byte[] bytes = BitConverter.GetBytes(iColor);
        Color color = Color.FromRgb(bytes[2], bytes[1], bytes[0]);

        return new SolidColorBrush(color);
    }

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