简体   繁体   English

MVVM-根据列名称隐藏Datagrid列,其中autogeneratecolumns = True

[英]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 我有一个绑定到DatatableDataGrid ,我想使用ID唯一标识DataTable中的行,但是我不希望它显示在DataGrid

What I reached so far by searching and excluding: 到目前为止,我通过搜索和排除以下内容达到了什么:

  1. Data columns are not predefined, so, I have to use AutoGenerateColumns=True , hence, I can't define the columns manually and set the Visibility property to False . 数据列不是预定义的,因此,我必须使用AutoGenerateColumns=True ,因此,我无法手动定义列并将Visibility属性设置为False
  2. I can't use List or ObservableCollection to define private ID member, because the data are dynamic. 我无法使用ListObservableCollection定义私有ID成员,因为数据是动态的。
  3. I am following 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 提前致谢

EDIT: 编辑:

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. DataGridCellDataGridColumnHeader在表的末尾分配该列,这从表的中间删除了空列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当AutoGenerateColumns设置为True时,如何停止在DataGrid中生成特定列? - how to stop a specific column to be generated in DataGrid when the AutoGenerateColumns is set to True? DataGrid AutoGenerateColumns =“True” - 如何附加一个额外的列? - DataGrid AutoGenerateColumns=“True” - how to append one extra column? 当AutoGenerateColumns =“ True”时,使datagrid的特定列成为组合框 - Make particular column of datagrid a combobox when AutoGenerateColumns=“True” 使用autogeneratecolumns = true重命名gridview中的列标题文本 - renaming column header texts in gridview with autogeneratecolumns = true 如何使用 MVVM 自动隐藏 WPF 中的 DataGrid 列? - How to hide DataGrid column in WPF automatically using MVVM? 如何从GridView中删除列,其中DataSource是DataTable,而AutoGenerateColumns为true? - How to delete column from GridView where DataSource is a DataTable and AutoGenerateColumns is true? MVVM将DataGrid绑定到DataTable显示错误的列名 - MVVM Binding DataGrid to DataTable shows wrong Column Name 仅当AutoGenerateColumns = true时,Datagrid才能工作 - Datagrid only works when AutoGenerateColumns = true 绑定datagrid列可见性MVVM - Bind datagrid column visibility MVVM DataGrid &lt;-&gt; ObservableCollection中的MVVM唯一列 - MVVM Unique column in DataGrid <-> ObservableCollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM