如何正确使用 ToggleButton.IsChecked 或 IsPressed 上的触发器?

发布于 2024-11-29 13:46:22 字数 2824 浏览 2 评论 0原文

这是我的代码:

<Window x:Class="WpfTests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Thickness x:Key="tasksMargin">0,10,0,0</Thickness>

</Window.Resources>
<Grid>
    <DockPanel HorizontalAlignment="Left">
        <!--<Border Padding="15">-->
        <GroupBox>
            <GroupBox.Header>
                Database Tasks
            </GroupBox.Header>
            <StackPanel Width="Auto" >
                <StackPanel.Resources>
                    <Style  TargetType="RadioButton">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="RadioButton">
                                    <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                      Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                  BorderThickness="2" BorderBrush="Yellow" Background="White"/>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                                                <Setter Property="Content" Value="different txt" />
                                            </Trigger>
                                        <Trigger Property="ToggleButton.IsPressed" Value="True">
                                                <Setter Property="Content" Value="different text" />
                                            </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Margin" Value="5">
                        </Setter>
                    </Style>
                </StackPanel.Resources>

                <RadioButton  GroupName="Group1"  x:Name="button1">Option1</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button2" x:Name="button2">button2</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button3" x:Name="button3">button3</RadioButton>
            </StackPanel>
        </GroupBox>
    </DockPanel>
</Grid>

但是,IsChecked 和 IsPressed 触发器不会被触发。如何正确声明它们?

That's my code:

<Window x:Class="WpfTests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Thickness x:Key="tasksMargin">0,10,0,0</Thickness>

</Window.Resources>
<Grid>
    <DockPanel HorizontalAlignment="Left">
        <!--<Border Padding="15">-->
        <GroupBox>
            <GroupBox.Header>
                Database Tasks
            </GroupBox.Header>
            <StackPanel Width="Auto" >
                <StackPanel.Resources>
                    <Style  TargetType="RadioButton">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="RadioButton">
                                    <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                      Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                  BorderThickness="2" BorderBrush="Yellow" Background="White"/>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                                                <Setter Property="Content" Value="different txt" />
                                            </Trigger>
                                        <Trigger Property="ToggleButton.IsPressed" Value="True">
                                                <Setter Property="Content" Value="different text" />
                                            </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Margin" Value="5">
                        </Setter>
                    </Style>
                </StackPanel.Resources>

                <RadioButton  GroupName="Group1"  x:Name="button1">Option1</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button2" x:Name="button2">button2</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button3" x:Name="button3">button3</RadioButton>
            </StackPanel>
        </GroupBox>
    </DockPanel>
</Grid>

However, the IsChecked and IsPressed triggers do not get triggered. how to I declare them properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尸血腥色 2024-12-06 13:46:22

如果您为 ToggleButton 指定一个名称,然后在触发器设置器中引用该名称,那么它应该可以工作。所以,你的 ControlTemplate 看起来像......

<ControlTemplate TargetType="RadioButton">
    <ToggleButton Name="toggleButton" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                  Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                              BorderThickness="2" BorderBrush="Yellow" Background="White"/>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsChecked" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different txt" />
        </Trigger>
        <Trigger Property="ToggleButton.IsPressed" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different text" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

If you give your ToggleButton a name and then reference that in the trigger setters then it should work. So, your ControlTemplate would look something like...

<ControlTemplate TargetType="RadioButton">
    <ToggleButton Name="toggleButton" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                  Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                              BorderThickness="2" BorderBrush="Yellow" Background="White"/>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsChecked" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different txt" />
        </Trigger>
        <Trigger Property="ToggleButton.IsPressed" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different text" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
总以为 2024-12-06 13:46:22

不需要触发器中的前缀类型,还将切换按钮上的绑定更改为 TemplateBindings

The prefixed types in the triggers are not needed, also change the bindings on the togglebutton to TemplateBindings.

软甜啾 2024-12-06 13:46:22

还有一些其他解决方案可用,但是对于这个问题。执行以下操作:

1) 给出您的切换名称

<ToggleButton Name="tglBtn" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              BorderThickness="2" BorderBrush="Yellow" Background="White"/>

2) 在触发器参考中按给出的名称

<Trigger SourceName="tglBtn" Property="IsChecked" Value="True">
      <Setter TargetName="tglBtn" Property="Content" Value="different txt" />
</Trigger>

希望有帮助

There some other solutions available, but for this problem. Do the following:

1) Give Your Toggle Name

<ToggleButton Name="tglBtn" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              BorderThickness="2" BorderBrush="Yellow" Background="White"/>

2) In trigger reference by the name given

<Trigger SourceName="tglBtn" Property="IsChecked" Value="True">
      <Setter TargetName="tglBtn" Property="Content" Value="different txt" />
</Trigger>

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文