wpf GridViewDataColumn通过多个属性绑定

时间:2018-12-18 09:56:55

标签: wpf binding

我正在使用WPF应用程序。我在XAML文件中有“ GridViewDataColumn”。在一种情况下,我必须通过两个属性来绑定“ GridViewDataColumn”的Invisible属性。请检查下面的代码(像这样)-

<telerik:GridViewDataColumn x:Name="NameColumn"
    IsVisible="{Binding IsNameEnabled}"
    IsVisible="{Binding SomeColumn,Converter={StaticResource SomeBoolConverter}, ConverterParameter={x:Static resources:SomeResources.Name}}"
</telerik:GridViewDataColumn>

但是它没有绑定,并给出了一些绑定错误。有人可以帮我解决如何通过多个属性绑定Invisible属性。

2 个答案:

答案 0 :(得分:1)

您不能像这样两次分配属性。您需要改用触发器。

<telerik:GridViewDataColumn x:Name="NameColumn">
    <telerik:GridViewDataColumn.Stlyle>
        <Style>
            <Setter Property="IsVisible" Value="True"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsNameEnabled}" Value="False">
                    <Setter Property="IsVisible" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding SomeColumn,Converter={StaticResource SomeBoolConverter}, ConverterParameter={x:Static resources:SomeResources.Name}}" Value="False">
                    <Setter Property="IsVisible" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </telerik:GridViewDataColumn.Stlyle>
</telerik:GridViewDataColumn>

答案 1 :(得分:1)

如果要绑定多个源属性,则应使用MultiBinding和实现IMultiValueConverter接口的转换器类,例如:

public class SomeBoolConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool isNameEnabled = System.Convert.ToBoolean(values[0]);
        bool someColumn = System.Convert.ToBoolean(values[1]);

        //...

        return isNameEnabled && someColumn;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

XAML:

<telerik:GridViewDataColumn x:Name="NameColumn">
    <telerik:GridViewDataColumn.IsVisible>
        <MultiBinding Converter="{StaticResource SomeBoolConverter}"
                      ConverterParameter="{x:Static resources:SomeResources.Name}">
            <Binding Path="IsNameEnabled" />
            <Binding Path="SomeColumn" />
        </MultiBinding>
    </telerik:GridViewDataColumn.IsVisible>
</telerik:GridViewDataColumn>