Show/Hide 来自 datacontext wpf 的所有数据网格列

Show/Hide all datagrid columns from datacontext wpf

我需要从 header 中的 context menu 更改 Datagrid columns visibility。默认情况下,所有列的可见性都在 XAML 代码中设置。

<DataGridTextColumn Visibility="Visible" Binding="{Binding Story}" Header="Story" />

我不想绑定 ViewModel 中的每一列可见性。 如果所有列都被隐藏,我想要将所有列的可见性设置为可见,反之亦然。全部来自 XAML 代码,未绑定列代码内的可见性。

I've added an empty column as a temporary solution and removed it when showing one or more columns, I've achieved what I wanted using the click event, by looping through all the columns and set their visibility. but I'm looking for a solution using Only XAML code.

MVVM 不是关于消除代码。这是关于关注点的分离。 XAML 是一种 标记 语言。在控件本身或视图中向 DataGrid 的 hide/display 列添加代码是完全没问题的。