如何将2个值传递给ValueConverters类?

时间:2011-04-11 09:22:04

标签: c# wpf binding wpfdatagrid

我已经在我的Xaml上得到了这个,我正在传递分配给改变gridcell颜色的值。但我想同时检查输入的变量值,这样我就可以根据那个改变。我怎么能传递2个值,这样我才能在我的iconverter类上有条件。

<DataGridTextColumn Binding="{Binding Allocated}" Header="Allocated" >
                     <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Background" Value="{Binding Allocated, Converter={StaticResource converter}}"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>     </DataGridTextColumn>

我的转换器类:

object IValueConverter.Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture){

         string input = value.ToString(); 

         switch (input) 
         { 
             case "99":
                 return Brushes.Green;
             case "96":
                 return Brushes.Green;
             case "91":
              default:   
                 return DependencyProperty.UnsetValue; 
         }

    }
  

块引用

提前致谢!

1 个答案:

答案 0 :(得分:2)

使用MultiBinding

<DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Background">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource converter}">
                    <Binding Path="Allocated" />
                    <Binding Path="Entered" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTextColumn.ElementStyle>

并制作你的召集工具IMultiValueConverter

public class MyConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, 
                          object parameter, CultureInfo culture)
    {...