[英]MVVM - Hide Datagrid column based on column name with autogeneratecolumns = True
I have a DataGrid
that's bound to Datatable
, and I want to uniquely identify rows in the DataTable
using the ID
, but I don't want it to be shown in the DataGrid
我有一个绑定到
Datatable
的DataGrid
,我想使用ID
唯一标识DataTable
中的行,但是我不希望它显示在DataGrid
What I reached so far by searching and excluding: 到目前为止,我通过搜索和排除以下内容达到了什么:
AutoGenerateColumns=True
, hence, I can't define the columns manually and set the Visibility
property to False
. AutoGenerateColumns=True
,因此,我无法手动定义列并将Visibility
属性设置为False
。 List
or ObservableCollection
to define private ID
member, because the data are dynamic. List
或ObservableCollection
定义私有ID
成员,因为数据是动态的。 MVVM
so, I can't use AutoGeneratingColumns
event handler directly and can't expose the View
to the ViewModel
. MVVM
因此,我无法直接使用AutoGeneratingColumns
事件处理程序,也无法将View
公开给ViewModel
。 The closest I get to an answer is using DataTrigger
to set Visibility
to False
using CellStyle
, but it just hid the cells, not the entire column, and I also tried it for DataGridColumnHeader
and it didn't work: 最近我得到的答案是使用
DataTrigger
设置Visibility
,以False
使用CellStyle
,但它只是隐藏单元格,而不是整个列,我也试了DataGridColumnHeader
和它没有工作:
code: 码:
<Style x:Key="ColumnStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="id">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
How to do it while maintaining the previous conditions? 在保持先前条件的同时如何做? Thanks in advance
提前致谢
I fixed the code for DataGridColumnHeader
using Path=Column.Header
which doesn't make sense to me but it's irrelevant; 我使用
Path=Column.Header
修复了DataGridColumnHeader
的代码,这对我来说没有意义,但是无关紧要; Still, there and empty column standing there, with no idea how to remove it. 尽管如此,那儿还有空的立柱,不知道如何将其移除。
It sounds like you want to track the selected Item. 听起来您想跟踪所选项目。 If you want to track the "selected Element", you have to use a CollectionView.
如果要跟踪“选定元素”,则必须使用CollectionView。
WPF controls do not direcly bind to collections. WPF控件不会直接绑定到集合。 They bind to a CollectionView.
它们绑定到CollectionView。 And if you do not give them one, they will create one themself from whatever collection you hand them.
而且,如果您不给他们一个礼物,他们会从您交给他们的任何收藏中创造一个自己的礼物。 If you want sorting, filtering, ordering and selection tracking, CollectionView is the droid you are looking for: https://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.aspx
如果要进行排序,过滤,排序和选择跟踪,则CollectionView是您要查找的机器人: https : //msdn.microsoft.com/zh-cn/library/system.windows.data.collectionview.aspx
Just take control of it's creation and expose it (rather then the raw collection). 只需控制它的创建并将其公开(而不是原始集合)即可。
I found a solution by applying this style: 我通过应用此样式找到了解决方案:
<Style x:Key="ColumnStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="id">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
to DataGridCell
and to DataGridColumnHeader
and allocating the column in the end of the table this removed the empty column from the middle of the table. 到
DataGridCell
和DataGridColumnHeader
并在表的末尾分配该列,这从表的中间删除了空列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.