在 WPF 数据网格列中加载不同的数据模板

Load different data template in WPF datagrid column

是否可以为 WPF 数据网格中定义的列加载不同的数据模板?

我的 XAML 看起来像这样:

<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>

此处的目标是在绑定 IsStarted 设置为 false 时加载单独的数据模板,换句话说,当可见性设置为 false 时。

这里的预期目的是当某个按钮被触发时,将布尔值设置为“false”,另一个数据模板将在这个自己的列上可见,而不是当前存在的项目。

例如,在执行按钮后将布尔值设置为 false 后,应显示以下 XAML,

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

这可能吗?

您可以将 DataTemplate 中的 CheckBox 替换为 ContentControl,并使用 StyleDataTrigger 替换其 ContentTemplate 基于IsStarted参数的值:

<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>