WPF:如何使用XAML隐藏GridViewColumn?

32
我在App.xaml中有以下对象。
<Application.Resources>
        <ResourceDictionary>
            <GridView x:Key="myGridView" x:Shared="false">
                             <GridViewColumn Header="Created" DisplayMemberBinding="{Binding Path=Created}"/>

... more code ...

我在多个地方使用这个网格视图。例如:

<ListView x:Name="detailList"   View="{StaticResource myGridView}" ...>
在其中一种用法中(例如上面的detailList),我想隐藏Created列,可能使用XAML实现?
有任何想法吗?
11个回答

0

在我编写的一个小型实用程序中,我有一个列表视图,用户可以隐藏/显示一些列。列上没有可见性属性,所以我决定将隐藏的列宽度设置为零。虽然不理想,因为用户仍然可以调整它们的大小并使它们重新可见。

无论如何,为了做到这一点,我使用了:

<GridViewColumn.Width>
    <MultiBinding Converter="{StaticResource WidthConverter}" Mode="TwoWay">
        <Binding Path="ThreadIdColumnWidth" Mode="TwoWay" />
        <Binding Path="IsThreadIdShown" />
    </MultiBinding>
</GridViewColumn.Width>

IsThreadIdShown绑定到工具栏上的复选框。 而多值转换器是:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
    if (values.Length != 2) {
        return null;
    }

    object o0 = values[0];
    object o1 = values[1];

    if (! (o1 is bool)) {
        return o0;
    }
    bool toBeDisplayed = (bool) o1;
    if (! toBeDisplayed) {
        return 0.0;
    }

    if (! (o0 is double)) {
        return 0;
    }

    return (double) o0;
}

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

    return new object[] { (double)value, Binding.DoNothing };
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接