How to dynamically change a WPF control's template using a checkbox?

后端 未结 2 765
有刺的猬
有刺的猬 2021-02-06 09:12

I have an error dialog (shown simplified below).

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

相关标签:
2条回答
  • 2021-02-06 09:22

    This Solution is for those who are searching for Template swap. It is simple hope it helps you. Please point out any mistakes.

    Just use this code for changing the Template on checkBox Checked Event.

     private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            DataTemplate Temp;
            Temp = (DataTemplate)this.FindResource("TemplateYouHaveCreated");
            listView1.ItemTemplate = Temp;
        }
    

    refer this link for more information

    http://developingfor.net/2009/01/09/dynamically-switch-wpf-datatemplate/

    0 讨论(0)
  • 2021-02-06 09:40

    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>
    

    Update

    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>
    
    0 讨论(0)
提交回复
热议问题