本文介绍了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?

我在事件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中生成的特定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:09