Hide Datagrid Column based on its Property name

99封情书 提交于 2019-11-29 16:22:04

You could handle the DataGrid.AutoGeneratedColumns Event and set the column's Visibility property from there. You should be able to do something like this:

private void DataGridAutoGeneratingColumn(object sender, 
    DataGridAutoGeneratingColumnEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    if (dataGrid != null && IsBKM) dataGrid.Columns[0].Visible = false;
}

UPDATE >>>

You can use the e.Column.Header property to check the name of the column and then use that instead. However, your column has no Header currently set. You could also set the column name (in XAML) and then check for that Name value instead of using the Header property:

private void DataGridAutoGeneratingColumn(object sender, 
    DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column.Name == "IsBKM" && IsBKM)
    {
        e.Column.Visibility = Visibility.Collapsed;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!