【问题标题】:Changing ControlTemplate when button clicked Wpf单击按钮时更改 ControlTemplate Wpf
【发布时间】:2017-12-29 18:48:07
【问题描述】:

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

它的作用是在按下按钮时更改模板。 但是,我希望它在单击后发生,而无需按住它。

1)点击样式是否有任何触发器,所以我点击的那一刻会调用触发器?

2) 在代码隐藏文件中,我希望它在单击按钮的那一刻运行一个函数,然后才更改模板,因为它使用的是第一个模板中的数据。

import wpf

from System.Windows import Application, Window
from scores import score

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

    def Button_Click1(self, sender, e):
        x = score(name)

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

所以最终我的目标是点击按钮,计算功能得分,然后才更改模板。

感谢您的帮助。

【问题讨论】:

  • 为什么不使用切换按钮?它有IsChecked
  • 因为这是一个简单的例子。当你写名字时,我真正的 GUI 基本上是一个登录屏幕,一旦你点击登录,我希望它改变模板。切换按钮看起来不会那么好。
  • 那么您需要使用布尔值。我没有其他办法
  • 你能告诉我在哪里使用这个布尔值吗?在什么属性上?
  • 确定@Ben,看看下面的答案

标签: python wpf visual-studio xaml mvvm


【解决方案1】:

在 MyWindow 中定义一个 DependencyProperty:(MyWindow.xaml.cs)

    public bool MyBoolean
    {
        get { return (bool)GetValue(MyBooleanProperty); }
        set { SetValue(MyBooleanProperty, value); }
    }
    public static readonly DependencyProperty MyBooleanProperty =
        DependencyProperty.Register("MyBoolean", typeof(bool), typeof(MyWindow), new PropertyMetadata(false));

当按钮被点击时,这个布尔值必须设置为真。

然后为你的 MyWindow 命名:

<Window x:Name="root"
        .../>

那么你的触发器会是这样的:

<DataTrigger Binding="{Binding ElementName=root,Path=MyBoolean}" Value="True">
    <Setter Property="Template" Value="{StaticResource detailedErrorTemplate}"/>
</DataTrigger>

【讨论】:

  • 感谢您的回答,我确定它可以使用 c# 工作,只有我在 ironpython 中使用 wpf 项目,并且据我所知,它不可能使用这种语法伙伴。但再次感谢。
猜你喜欢
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多