Home > Software engineering >  Load different data template in WPF datagrid column
Load different data template in WPF datagrid column

Time:11-25

Is it possible to load a different data template for a defined column in a WPF data grid?

My XAML looks like this:

<DataGridTemplateColumn Header="Select">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox 
                  HorizontalContentAlignment="Center"
                  Visibility="{Binding IsStarted}"
                  VerticalAlignment="Center"
                  IsChecked="{Binding IsStarted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  Command="{Binding DataContext.Checked,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                  CommandParameter="{Binding}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>

The goal here is load a separate data template when the binding IsStarted is set to false, In other words when the visibility is set to false.

The intended purpose here is when a certain button is triggered which will set the boolean to "false" another data template will be visible on this very own column instead of the currently existing items.

As an example, the following XAML should be displayed once the boolean is set to false after the execution of the button,

<TextBlock Visibility="{Binding IsTrue}" Text="Hello" />

Is this possible?

CodePudding user response:

You could replace the CheckBox in the DataTemplate with a ContentControl and use a Style with a DataTrigger to replace its ContentTemplate based on the value of the IsStarted parameter:

<DataGridTemplateColumn Header="Select">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding}">
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <CheckBox 
                                        HorizontalContentAlignment="Center"
                                        Visibility="{Binding IsStarted}"
                                        VerticalAlignment="Center"
                                        IsChecked="{Binding IsStarted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                        Command="{Binding DataContext.Checked,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                        CommandParameter="{Binding}"/>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsStarted}" Value="False">
                                <Setter Property="ContentTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="Some other template" />
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
  • Related