Wpf: 在 Listview 中隐藏 GridViewColumn

Wpf: Hide GridViewColumn in Listview

我在 Listview 中有一个 GridView

而且Griview里面有好几个GridViewColumn。

现在我想隐藏其中一列

我写了这个:

<Grid>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First" Width="100"/>
                    <GridViewColumn Header="Second" Width="0"/>
                    <GridViewColumn Header="Third" Width="100"/>
                    <GridViewColumn Header="Fourth" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

Initially it is hidden, though i can still see the second column if i extend the third column separator.

有什么方法可以设置 GridViewColumn 的可见性吗?

请尝试下一个解决方案。由于 DataGridColumns 不在 DataGrid 可视化树中,因此您必须使用变通方法来实现对可见性等属性的绑定。换句话说,您需要使用代理。这是对我有用的方法。 1. Xaml代码:

<Window x:Class="DataGridSoHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dataGridSoHelpAttempt="clr-namespace:DataGridSoHelpAttempt"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    Title="MainWindow" Height="350" Width="525" x:Name="This">
<Window.DataContext>
    <dataGridSoHelpAttempt:MainViewModel/>
</Window.DataContext>
<Grid x:Name="MyGrid">
    <Grid.Resources>
        <dataGridSoHelpAttempt:FreezableProxyClass x:Key="ProxyElement" ProxiedDataContext="{Binding Source={x:Reference This}, Path=DataContext}"/>
    </Grid.Resources>
    <DataGrid x:Name="MyDataGrid" ItemsSource="{Binding DataSource}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Description" Binding="{Binding Description}"  Visibility="{Binding Source={StaticResource ProxyElement}, 
                Path=ProxiedDataContext.Visibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
            <DataGridTextColumn Header="Comments" Binding="{Binding Comments}" Width="150"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Content="Show Description" Command="{Binding Command}"></Button>
</Grid>

2.FreezableProxy类代码:

public class FreezableProxyClass : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new FreezableProxyClass();
    }


    public static readonly DependencyProperty ProxiedDataContextProperty = DependencyProperty.Register(
        "ProxiedDataContext", typeof (object), typeof (FreezableProxyClass), new PropertyMetadata(default(object)));

    public object ProxiedDataContext
    {
        get { return (object) GetValue(ProxiedDataContextProperty); }
        set { SetValue(ProxiedDataContextProperty, value); }
    }
}

3。视图模型代码:

    public class MainViewModel:BaseObservableObject
{
    private Visibility _visibility;
    private ICommand _command;

    public MainViewModel()
    {
        Visibility = Visibility.Collapsed;
        DataSource = new ObservableCollection<BaseData>(new List<BaseData>
        {
            new BaseData {Name = "Uncle Vania", Description = "A.Chekhov, play", Comments = "worth reading"},
            new BaseData {Name = "Anna Karenine", Description = "L.Tolstoy, roman", Comments = "worth reading"},
            new BaseData {Name = "The Master and Margarita", Description = "M.Bulgakov, novel", Comments = "worth reading"},
        });
    }

    public ICommand Command
    {
        get
        {
            return _command ?? (_command = new RelayCommand(VisibilityChangingCommand));
        }
    }

    private void VisibilityChangingCommand()
    {
        Visibility = Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
    }

    public ObservableCollection<BaseData> DataSource { get; set; }

    public Visibility Visibility
    {
        get { return _visibility; }
        set
        {
            _visibility = value;
            OnPropertyChanged();
        }
    }
}

public class BaseData:BaseObservableObject
{
    private string _name;
    private string _description;
    private string _comments;

    public virtual string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public virtual object Description
    {
        get { return _description; }
        set
        {
            _description = (string) value;
            OnPropertyChanged();
        }
    }

    public string Comments
    {
        get { return _comments; }
        set
        {
            _comments = value;
            OnPropertyChanged();
        }
    }
}

4。 MVVM 部分

    public class BaseObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
    {
        var propName = ((MemberExpression)raiser.Body).Member.Name;
        OnPropertyChanged(propName);
    }

    protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(name);
            return true;
        }
        return false;
    }
}

public class RelayCommand : ICommand
{
    private readonly Func<bool> _canExecute;
    private readonly Action _execute;

    public RelayCommand(Action execute)
        : this(() => true, execute)
    {
    }

    public RelayCommand(Func<bool> canExecute, Action execute)
    {
        _canExecute = canExecute;
        _execute = execute;
    }

    public bool CanExecute(object parameter = null)
    {
        return _canExecute();
    }

    public void Execute(object parameter = null)
    {
        _execute();
    }

    public event EventHandler CanExecuteChanged;
}

5。它看起来如何:.

如果您在使用代码时遇到问题,我很乐意提供帮助。 问候。

更新 1. Xaml代码:

<Window x:Class="DataGridSoHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dataGridSoHelpAttempt="clr-namespace:DataGridSoHelpAttempt"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    Title="MainWindow" Height="350" Width="525" x:Name="This">
<Window.Resources>
    <dataGridSoHelpAttempt:ColumnObject2ColumnConverter x:Key="ColumnObject2ColumnConverterKey" />
</Window.Resources>
<Window.DataContext>
    <dataGridSoHelpAttempt:MainViewModel/>
</Window.DataContext>
<Grid x:Name="MyGrid">
    <Grid.Resources>
        <dataGridSoHelpAttempt:FreezableProxyClass x:Key="ProxyElement" ProxiedDataContext="{Binding Source={x:Reference This}, Path=DataContext}"/>
    </Grid.Resources>
    <Grid>
        <ListView ItemsSource="{Binding DataSource}">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"/>
                        <dataGridSoHelpAttempt:MyGridViewColumn Visibility="{Binding Visibility}" DefaultWidth="100" Header="Description" DisplayMemberBinding="{Binding Description}" Width="100"/>
                        <GridViewColumn Header="Comments" DisplayMemberBinding="{Binding Comments}" Width="100"/>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
    <!--<DataGrid x:Name="MyDataGrid" ItemsSource="{Binding DataSource}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Description" Binding="{Binding Description}"  Visibility="{Binding Source={StaticResource ProxyElement}, 
                Path=ProxiedDataContext.Visibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
            <DataGridTextColumn Header="Comments" Binding="{Binding Comments}" Width="150"/>
        </DataGrid.Columns>
    </DataGrid>-->
    <Button HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Content="Show Description" Command="{Binding Command}"></Button>
</Grid></Window>

2。 MyGridViewColumn 代码:

public class MyGridViewColumn : GridViewColumn
{

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
    }

    public static readonly DependencyProperty DefaultWidthProperty = DependencyProperty.Register(
        "DefaultWidth", typeof (double), typeof (MyGridViewColumn), new PropertyMetadata(default(double)));

    public double DefaultWidth
    {
        get { return (double) GetValue(DefaultWidthProperty); }
        set { SetValue(DefaultWidthProperty, value); }
    }

    /// <summary>
    /// Width DependencyProperty
    /// </summary>
    public new static readonly DependencyProperty WidthProperty =
        FrameworkElement.WidthProperty.AddOwner(
            typeof(MyGridViewColumn),
            new PropertyMetadata(
                Double.NaN /* default value */,
                new PropertyChangedCallback(OnWidthChanged))
        );


    public static readonly DependencyProperty VisibilityProperty =
        UIElement.VisibilityProperty.AddOwner(typeof (MyGridViewColumn),
            new PropertyMetadata(Visibility.Visible, new PropertyChangedCallback(Target)));

    private static double _zeroValue = 0d;

    private static void Target(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        var column = dependencyObject as MyGridViewColumn;
        var visibility = (Visibility)args.NewValue;
        if (visibility == Visibility.Collapsed)
        {
            column.SetValue(WidthProperty, _zeroValue);
        }
        else if(visibility == Visibility.Visible)
        {
            column.SetValue(WidthProperty, column.DefaultWidth);
        }
    }

    public Visibility Visibility
    {
        get { return (Visibility) GetValue(VisibilityProperty); }
        set { SetValue(VisibilityProperty, value); }
    }



    /// <summary>
    /// width of the column
    /// </summary>
    /// <remarks>
    /// The default value is Double.NaN which means size to max visible item width.
    /// </remarks>
    [TypeConverter(typeof(LengthConverter))]
    public double Width
    {
        get { return (double)GetValue(WidthProperty); }
        set { SetValue(WidthProperty, value); }
    }

    private static void OnWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyGridViewColumn c = (MyGridViewColumn)d;

        var visibility = c.Visibility;
        if(visibility == Visibility.Collapsed)
            c.Width = _zeroValue;

        double newWidth = (double)e.NewValue;
        c.OnPropertyChanged(new PropertyChangedEventArgs(WidthProperty.Name));
    }
}

它的样子:

此致。