单击Wp按钮时更改ControlTemplate

2024-09-29 21:58:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下简单的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)在代码隐藏文件中,我希望它在我单击按钮时运行一个函数,然后才更改模板,因为它使用的是来自第一个模板的数据。在

^{pr2}$

所以最终我的目标是点击按钮,计算函数的分数,然后改变模板。在

谢谢你的帮助。在


Tags: textmargin模板autovaluestylebuttonwindow
1条回答
网友
1楼 · 发布于 2024-09-29 21:58:54

在MyWindow中定义DependencyProperty:(窗口.myxacs)在

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

单击按钮时,必须将布尔值设置为true。在

然后将您的MyWindow命名为:

^{pr2}$

那么你的触发器应该是:

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

相关问题 更多 >

    热门问题