【问题标题】:How to use a trigger on ToggleButton.IsChecked or IsPressed correctly?如何在 ToggleButton.IsChecked 或 IsPressed 上正确使用触发器?
【发布时间】:2011-10-28 02:19:39
【问题描述】:

这是我的代码:

<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 触发器。如何正确申报?

【问题讨论】:

  • 如果我是你,我会从网上获取一些有效的代码副本,然后从那里获取。例如:anders.janmyr.com/2007/09/…
  • 而对我真正有用的答案刚刚被删除。现在有两个反对票:D
  • @Greg - 它对你有用吗?我删除了它,因为我注意到它只是部分工作。 IsPressed 触发器不起作用。我已经恢复了答案 - 但我认为还有更多工作要做!
  • 是的,被按下似乎不起作用。但 IsChecked 有效,这是最低要求。如果 IsPressed 的解决方案突然出现在您的脑海中 - 不要犹豫更新您的答案:)

标签: c# wpf xaml styles togglebutton


【解决方案1】:

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

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>

希望有帮助

【讨论】:

    【解决方案2】:

    如果你给你的 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>
    

    【讨论】:

      【解决方案3】:

      触发器中的前缀类型不需要,也将togglebutton上的绑定更改为TemplateBindings。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-14
        • 2016-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多