这是我的代码:
<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触发器不会被触发。如何正确申报?
答案 0 :(得分:4)
如果你给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>
答案 1 :(得分:1)
不需要触发器中的前缀类型,也可以将togglebutton上的绑定更改为TemplateBindings
。
答案 2 :(得分:0)
还有一些其他解决方案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>
希望有所帮助