【问题标题】:Load different data template in WPF datagrid column在 WPF 数据网格列中加载不同的数据模板
【发布时间】:2021-12-31 20:04:57
【问题描述】:

是否可以为 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" />

这可能吗?

【问题讨论】:

  • 您可以定义一个DataGridTemplateColumn.CellTemplateSelector。这是一个例子:TemplateSelector

标签: wpf xaml binding datagrid


【解决方案1】:

您可以将DataTemplate 中的CheckBox 替换为ContentControl,并根据IsStarted 参数的值使用Style 和DataTrigger 替换其ContentTemplate:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多