AutoGenerateColumns设置为True时,如何停止要在DataGrid中生成的特定列? [英] how to stop a specific column to be generated in DataGrid when the AutoGenerateColumns is set to True?

查看:111
本文介绍了AutoGenerateColumns设置为True时,如何停止要在DataGrid中生成的特定列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将ObservableCollection绑定到 DataGrid ,并在带有MVVM应用程序的WPF中将 AutoGenerateColumns 设置为true。

I have bound a ObservableCollection to a DataGrid and set the AutoGenerateColumns to true in a WPF with MVVM application.

然后如何停止特定于的列会出现在DataGrid中吗?

Then how can I stop a specific column to be appeared in the DataGrid?

我在这里。但是我正在寻找更多的MVVM方法。

I have seen a same question here. But I'm looking for more MVVM approach.

推荐答案

MVVM意味着您的UI和数据层是完全独立的,并且使用View

MVVM means your UI and your Data layers are completely separate, with the View layer merely being a visual reflection of your Data layer.

因此,排除列的 MVVM方法取决于排除的位置:

So the "MVVM way" of excluding a column depends on where this exclusion should occur:


  • 如果您的数据层应排除一列,请从数据层中删除该列。

  • If your Data layer is supposed to exclude a column, remove that column from the data layer.

这通常意味着修改您的 DataGrid.ItemsSource 绑定到的集合,因此它不再包含不应该看到的数据。

This typically means modifying the collection your DataGrid.ItemsSource is binding to so it no longer includes the data that should not be visible.

或者取决于您的应用程序用于什么以及什么算作应用程序逻辑,这可能意味着维护字符串列出要排除的列的< string> 列,并让View找到某种方式绑定到这些字符串并修改其显示以排除那些列(自定义依赖项属性,转换器,重用o f Tag 属性+ AutoGeneratingColumn 事件,触发器等)

Or depending on what your application is for and what counts as "application logic", it may mean maintaining a string or List<string> of columns to exclude, and have the View find some way of binding to those strings and modifying it's display to exclude those columns (a custom dependency property, a converter, re-use of the Tag property + AutoGeneratingColumn event, triggers, etc)

如果您的View层应该排除一列,则从视图层中删除该列。

If your View layer is supposed to exclude a column, remove that column from the view layer.

通常通过设置 AutoGenerateColumns = False 并自行指定< DataGrid.Columns> ,但是您也可以使用以下方法从视图层中排除该列<$的 AutoGeneratingColumn 事件c $ c> DataGrid ,如果列的 ColumnName 等于要排除的值,则取消该列的生成,例如建议您链接的问题

This is usually done by setting AutoGenerateColumns="False" and specifying your <DataGrid.Columns> yourself, however you can also exclude the column from the View layer using the AutoGeneratingColumn event of the DataGrid, and cancelling the generation of the column if it's ColumnName equals to the value you want to exclude, like the question you linked suggested.

private void DataGrid_AutoGeneratingColumn(
    object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if ((string)e.Column.Header == "ID")
    {
        e.Cancel = true;
    }
}


请记住,MVVM的重点是将您的UI和数据层分开。在MVVM中,在视图后方使用代码绝对没有错,只要该代码仅与特定于UI的逻辑有关,而不与数据/应用程序特定的逻辑有关

Remember, the whole point of MVVM is to separate your UI and Data layers. There's absolutely nothing wrong with using code-behind the view in MVVM, providing that code is related to UI-specific logic only, and not data/application-specific logic

这篇关于AutoGenerateColumns设置为True时,如何停止要在DataGrid中生成的特定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆