更改按钮上的内容模板单击Wpf

问题描述 投票:-1回答:1

我有以下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>

当我点击按钮时,我想改变GUI样式就像在为Checkbox设置DataTrigger时发生的那样。

代码Behind看起来如下,但是当我点击按钮时窗口关闭而没有结果。

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

为什么模板不会改变?

谢谢。

wpf visual-studio xaml mvvm ironpython
1个回答
0
投票

我不熟悉ironpython,但在你的点击处理程序中,你看起来好像在设置窗口的ContentTemplate,这似乎是不正确的。我假设您的detailedErrorTemplate应该适用于您的ContentControl。在这种情况下,您需要将x:Name添加到ContentControl(即:x:Name =“TheContentControl”),以便在您的代码中引用ContentControl并设置(在c#中)

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

您有资源的密钥,而不是名称,因此您需要通过密钥索引资源字典。

根据评论编辑详细信息。

  1. 在你的xaml中,改变你的行 <ContentControl>

 <ContentControl x:Name="TheContentControl">
  1. 由于我们现在有一个ContentControl的名称,你可以正确地改变ContentControl的ControlTemplate(不是窗口。我知道你的ContentControl有效地包含了Window,但你不能只是将用于ContentControl的ControlTemplate分配给Window像那样)。更改您的代码行: self.ContentTemplate = Window.Resources.FindName(self,'detailedErrorTemplate')

to(我希望我正确地做了语法)。

 self.TheContentControl.Template = self.Resources['detailedErrorTemplate']
© www.soinside.com 2019 - 2024. All rights reserved.