Search code examples
wpfvisual-studioxamlmvvmironpython

Change Content Template on Button Click Wpf


I have the following XAML:

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="WpfApplication4" Height="300" Width="300">
    <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>
        <ContentControl>
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Setter Property="Template"
                        Value="{StaticResource simpleErrorTemplate}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=Button,Path=IsPressed}" Value="True">
                            <Setter Property="Template" Value="{StaticResource detailedErrorTemplate}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
        <Button x:Name="Button" Content="Button" Height="40" Width="129" Margin="88,5,76,5" Grid.Row="1" Click="Button_Click1"/>
    </Grid>
</Window>

I had like to change the GUI style the moment I click the button just like it happends when DataTrigger is set for the Checkbox.

The code Behind looks as follows however the moment I click the button the window closes and no result.

import wpf
from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        self.ui = wpf.LoadComponent(self, 'WpfApplication4.xaml')

    def Button_Click1(self, sender, e):
        self.ContentTemplate = Window.Resources.FindName(self,'detailedErrorTemplate') 

if __name__ == '__main__':
    Application().Run(MyWindow())

Why the template wont change?

Thank you.


Solution

  • I am unfamiliar with ironpython, but in your click handler it looks like you're setting the ContentTemplate of your window, which seems incorrect. I assume your detailedErrorTemplate is supposed to apply to your ContentControl. In this case you need to add x:Name to your ContentControl (i.e.: x:Name="TheContentControl") in order to reference the ContentControl in your code behind and set (in c#)

    TheContentControl.Template = this.Resources["detailedErrorTemplate"] as ControlTemplate;
    

    You have key's for your resources, not Names, so you need to index the resource dictionary by the key.

    Edit details based on comments.

    1. In your xaml, change your line

      <ContentControl>
      

    to

     <ContentControl x:Name="TheContentControl">
    
    1. Since we now have a name for the ContentControl, you can properly change the ControlTemplate of the ContentControl (NOT the window. I understand that your ContentControl effectively encompasses the Window, but you can't just assign that ControlTemplate meant for the ContentControl to the Window like that). Change your line of code from:

      self.ContentTemplate = Window.Resources.FindName(self,'detailedErrorTemplate')
      

    to (I hope I'm doing the syntax correctly).

     self.TheContentControl.Template = self.Resources['detailedErrorTemplate']