我有以下用于切换按钮的 xaml:
<ToggleButton Margin="0,3" Grid.Row="3" Grid.ColumnSpan="2" Command="{Binding DataContext.SelectAllCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Content" Value="Select All"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="Select None"/>
<Setter Property="Command" Value="{Binding DataContext.SelectNoneCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
但是单击按钮时,IsChecked
属性永远不会更新。
如果我使用 snoop 并强制 IsChecked 为 True,那么触发器就会启动。
最佳答案
我尝试了你的 ToggleButton,它工作正常。我看到的唯一问题是您明确设置了 Command。应该使用 Setter
来完成(就像您对 Content
所做的那样),以免破坏触发器
。
<ToggleButton Name="toggleButton" Margin="0,3" Grid.Row="3" Grid.ColumnSpan="2">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Content" Value="Select All"/>
<Setter Property="Command"
Value="{Binding DataContext.SelectAllCommand,
Mode=OneWay,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="Select None"/>
<Setter Property="Command" Value="{Binding DataContext.SelectNoneCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
如果您仍然无法单击它,我会检查可视化树中的 IsHitTestVisible
或类似内容是否设置为 False
。
如果您想将您的版本与我的测试应用程序进行比较,看看哪些地方不起作用,我将其上传到此处:http://www.mediafire.com/?ik24ftsfw2wwfwb
关于wpf - 为什么此切换按钮上的 IsChecked 不会更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4617937/