根据其属性名称隐藏Datagrid列 [英] Hide Datagrid Column based on its Property name
问题描述
我有一个 DataGrid
定义如下:
<DataGrid Name="dtMydatagrid" Margin="10,10,10,10" RowHeight="20" AutoGenerateColumns="True" ItemsSource="{Binding}" Height="auto" Width="auto">
<DataGrid.Columns>
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="TXT" Background="Transparent" Width="15" IsReadOnly="True" Visibility="Hidden" Margin="0,0,0,0"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsBKM}" Value="true">
<Setter Property="Background" Value="AQUA" TargetName="TXT"/>
<Setter Property="Visibility" Value="Visible" TargetName="TXT"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
现在,我的类名为 IsBKM
其中 DataGridTemplateColumn
是有界的。
所以,它被显示为 CheckBox
。
我不想在我的 DataGrid
中显示 IsBKM
列。我可以使用触发器并隐藏名称为 IsBKM
或 任何不同的解决方案 的列?
Now, I have a boolean property in my class named IsBKM
to which the DataGridTemplateColumn
is bounded.
So, it is displayed by as CheckBox
.
I don't want to display the IsBKM
column in my DataGrid
. Can I use a trigger and hide the column whose name is IsBKM
or any different solution?
提前感谢
推荐答案
你可以处理 DataGrid.AutoGeneratedColumns
事件,并从中设置列的可见性
属性。你应该可以这样做:
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 >>>
您可以使用 e.Column.Header
属性来检查列的名称,然后使用它。但是,您的列目前没有设置标题
。您可以还可以设置列名称(在XAML中),然后检查 Name
值,而不是使用头文件
property:
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;
}
}
这篇关于根据其属性名称隐藏Datagrid列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!