admin管理员组文章数量:1122833
I have a DataGrid (WPF) with the ItemsSource ObservableCollection Notifications. The DataGridTextColumn is bound to the property TestMessage of Notifications. This works. Now I want to set the Visibility property of the DataGridTextColumn Collapsed if TestMessage is null (x:Null). The check if TestMessage is null (x:Null) should be done with a trigger in xaml and not in the ViewModel. DataGridTextColumn has no property Style. Which style (ElementStyle, CellStyle, ...) do I have to use? Does anyone have an idea?
<DataGrid
ItemsSource="{Binding Notifications, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn
Header="Test Message"
Binding="{Binding TestMessage}">
<DataGridTextColumn.???>
</DataGridTextColumn.>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
I have a DataGrid (WPF) with the ItemsSource ObservableCollection Notifications. The DataGridTextColumn is bound to the property TestMessage of Notifications. This works. Now I want to set the Visibility property of the DataGridTextColumn Collapsed if TestMessage is null (x:Null). The check if TestMessage is null (x:Null) should be done with a trigger in xaml and not in the ViewModel. DataGridTextColumn has no property Style. Which style (ElementStyle, CellStyle, ...) do I have to use? Does anyone have an idea?
<DataGrid
ItemsSource="{Binding Notifications, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn
Header="Test Message"
Binding="{Binding TestMessage}">
<DataGridTextColumn.???>
</DataGridTextColumn.>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Share
Improve this question
asked Nov 21, 2024 at 11:03
TommerTommer
293 bronze badges
2
|
1 Answer
Reset to default -1In WPF, directly setting the Visibility of a DataGridTextColumn to Collapsed using XAML triggers is not possible because DataGridTextColumn does not have a Visibility property. However, you can achieve the desired behavior by using a DataGridTemplateColumn instead of a DataGridTextColumn. This allows you to dynamically control the Visibility using bindings and triggers.
Here's how you can do it: Replace the DataGridTextColumn with a DataGridTemplateColumn and use a DataTrigger to control the Visibility of the column.
<DataGrid
ItemsSource="{Binding Notifications, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Test Message">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TestMessage}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.Visibility>
<Binding Path="TestMessage" RelativeSource="{RelativeSource AncestorType={x:Type DataGrid}}">
<Binding.Converter>
<local:NullToVisibilityConverter />
</Binding.Converter>
</Binding>
</DataGridTemplateColumn.Visibility>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
本文标签: cHow to set the Visibility property of DataGridTextColumn with trigger in xamlStack Overflow
版权声明:本文标题:c# - How to set the Visibility property of DataGridTextColumn with trigger in xaml? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311541a1934775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
UpdateSourceTrigger=PropertyChanged
andMode=OneWay
on the ItemsSource Binding are both pointless. The Binding is inherently OneWay, and UpdateSourceTrigger has no effect on OneWay Bindings. – Clemens Commented Nov 22, 2024 at 7:31