在xaml中使用ConverterParameter

时间:2015-11-25 05:47:19

标签: c# wpf xaml

我的xaml中有一个数据网格:

<dg:DataGridTextColumn Header="A"
                       Width="110"
                       Binding="{Binding ValueA, StringFormat=N2}"
                       IsReadOnly="true"
                       FontWeight="Bold">
    <dg:DataGridTextColumn.CellStyle>
        <Style TargetType="dg:DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ValueA, Converter={StaticResource MyConverterA}}"
                             Value="True">
                    <Setter Property="Background"
                            Value="LightYellow" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </dg:DataGridTextColumn.CellStyle>
</dg:DataGridTextColumn>
<dg:DataGridTextColumn Header="B"
                       Width="180"
                       Binding="{Binding B, StringFormat=N2}"
                       IsReadOnly="true"
                       FontWeight="Bold">
    <dg:DataGridTextColumn.CellStyle>
        <Style TargetType="dg:DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding MyVal, Converter={StaticResource MyConverterB}, **ConverterParameter=??**}"
                             Value="True">
                    <Setter Property="Background"
                            Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </dg:DataGridTextColumn.CellStyle>
</dg:DataGridTextColumn>

我需要传递Column&#34; A&#34;的值。作为MyConverterB的参数,使用ConverterParameter。 任何指针都很好吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

在这种情况下,您可以使用MultiBinding代替ConverterParameter

<MultiBinding Converter="{StaticResource MyConverterB}">
    <Binding Path="A" />
    <Binding Path="B" />
</MultiBinding>

为此创建IMultiValueConverter

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

    }

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

    }
}

<强>更新

或者您可以尝试使用MultiDataTrigger来更改颜色。也许有些转换器。条件与逻辑AND一样工作。

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Path=ValueA}" Value="True" />
        <Condition Binding="{Binding Path=B}" Value="True" />
    </MultiDataTrigger.Conditions>
    <Setter Property="Background" Value="Red" />
</MultiDataTrigger>