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

标签 c# wpf xaml styles togglebutton

这是我的代码:

<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 触发器不会被触发。如何正确声明它们?

最佳答案

如果您为 ToggleButton 指定一个名称,然后在触发器 setter 中引用该名称,那么它应该可以工作。所以,你的 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>

关于c# - 如何正确使用 ToggleButton.IsChecked 或 IsPressed 上的触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7079210/

相关文章:

c# - 如何验证字典参数?

c# - 动态调用 DLL 中的方法

c# - CsvHelper 未按预期写入文件

wpf - 我可以在不知道绑定(bind)本身的情况下更改 DataTrigger 中绑定(bind)的属性吗?

带有 ItemsPresenter 的 WPF FlowDocument

c# - 使用 ViewModel (MVVM) 在当前 View 的子用户控件中设置属性

c# - 在 WPF 中将按钮绑定(bind)到 bool 值

c# - 如何使用 ItemsSource 在 DataGridCell 模板中绑定(bind) TextBlock.Text

c# - 有没有办法使用 DotTrace 获取单个方法调用的执行时间?

c# - WP7 (Windows phone 7) 在 XAML 或 C# 中锁定手机方向