如何使用复选框动态更改WPF控件的模板

如何使用复选框动态更改WPF控件的模板

本文介绍了如何使用复选框动态更改WPF控件的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误对话框(如下所示简化).

I have an error dialog (shown simplified below).

我在 ContentControl 中显示了 Report 对象,该对象已定义了 Template simpleErrorTemplate .

I display the Report object in a ContentControl to which I have defined a Template simpleErrorTemplate.

在窗口上有一个 CheckBox ,我想用它来将模板更改为 detailedErrorTemplate .实现此目标的最佳方法是什么?

There is a CheckBox on the Window that I would like to use to change the template to/from detailedErrorTemplate. What is the best way to achieve this?

<Window x:Class="Core.ErrorDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>

        <ControlTemplate x:Key="simpleErrorTemplate">
            <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="{Binding Message}" />
        </ControlTemplate>

        <ControlTemplate x:Key="detailedErrorTemplate">
            <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="{Binding Message}" />
            <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="{Binding Details}" />
            <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="{Binding StackTrace}" />
        </ControlTemplate>

    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>

        <ContentControl Grid.Row="0" Template="{StaticResource simpleErrorTemplate}" DataContext="{Binding Report}"/>

        <CheckBox Margin="10,0,0,0" Grid.Row="1" x:Name="ChkShowDetails">Show Details</CheckBox>
    </Grid>
</Window>

推荐答案

可以使用ContentControl样式的DataTrigger绑定到ChkShowDetails CheckBox

You can use a DataTrigger in the ContentControl Style where you bind to the IsChecked property of the ChkShowDetails CheckBox

<ContentControl Grid.Row="0" DataContext="{Binding Report}">
    <ContentControl.Style>
        <Style TargetType="ContentControl">
            <Setter Property="Template"
                    Value="{StaticResource simpleErrorTemplate}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ChkShowDetails,
                                               Path=IsChecked}"
                             Value="True">
                    <Setter Property="Template"
                            Value="{StaticResource detailedErrorTemplate}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>

更新

完整的Xaml示例,将其粘贴并尝试:)

Complete Xaml example, paste it and try it :)

<Window.Resources>
    <ControlTemplate x:Key="simpleErrorTemplate">
        <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="T1" />
    </ControlTemplate>
    <ControlTemplate x:Key="detailedErrorTemplate">
        <StackPanel>
            <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="T2" />
            <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="T3" />
            <TextBox Margin="10,10,10,5" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Text="T4" />
        </StackPanel>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
    <ContentControl Grid.Row="0" DataContext="{Binding Report}">
        <ContentControl.Style>
            <Style TargetType="ContentControl">
                <Setter Property="Template"
                        Value="{StaticResource simpleErrorTemplate}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=ChkShowDetails,
                                                   Path=IsChecked}"
                                 Value="True">
                        <Setter Property="Template"
                                Value="{StaticResource detailedErrorTemplate}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
    <CheckBox Margin="10,0,0,0" Grid.Row="1" x:Name="ChkShowDetails">Show Details</CheckBox>
</Grid>

这篇关于如何使用复选框动态更改WPF控件的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:04