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
  • How is this supposed to work? There is an item per row with a TestMessage property. Which of the items should control the visibility of the column? – Clemens Commented Nov 21, 2024 at 16:01
  • Also note that setting UpdateSourceTrigger=PropertyChanged and Mode=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
Add a comment  | 

1 Answer 1

Reset to default -1

In 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