5

我有以下用于切换按钮的 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,那么触发器就会启动。

4

3 回答 3

5

我试过你的 ToggleButton,它工作正常。我看到的唯一问题是您明确设置了 Command 。应该用 aSetter代替(就像你用 做的那样Content)来不破坏Trigger.

<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

于 2011-01-06T18:57:41.543 回答
2

通过参数化我的命令、绑定到我的视图模型上的新属性并将命令移动到样式中来修复它:

               <ToggleButton Margin="0,3" Grid.Row="3" Grid.ColumnSpan="2" IsThreeState="False" 
                            IsChecked="{Binding DataContext.IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                    <ToggleButton.Style>
                        <Style TargetType="{x:Type ToggleButton}">
                            <Setter Property="Content" Value="Select All"/>
                            <Setter Property="Command" Value="{Binding DataContext.SelectAllCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>                                
                            <Style.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Content" Value="Select None"/>                                
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>

有时您不只是喜欢 WPF!

于 2011-01-06T19:07:48.113 回答
1

只是一个猜测,但删除绑定并重Mode=OneWay试。

于 2011-01-06T17:51:26.873 回答