wpf 三态按钮行为(默认、按下、禁用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15301401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:58:18  来源:igfitidea点击:

3-state button behavior (default, pressed, disabled)

wpfbuttontriggers

提问by Bent Rasmussen

What's the idiomatic (and preferably easiest) way to implement a 3-state button where the states are:

什么是实现三态按钮的惯用(最好是最简单的)方法,其中状态是:

  • default
  • pressing
  • disabled
  • 默认
  • 紧迫
  • 残疾

So far I have this:

到目前为止,我有这个:

<Button ToolTip="Back" FontWeight="Bold" Command="{Binding Path=Navigator.GoBackCommand}" IsEnabled="{Binding Path=Navigator.CanGoBack}">
  <Viewbox Width="10">
    <ContentControl Content="{StaticResource ResourceKey=CoolLeftArrow}"/>
  </Viewbox>
</Button>

Is it necessary to replace the control template to get such behavior or can it be gotten using triggers? Ideally I just want to specify 3 different resources and bind them to the relevant properties.

是否有必要更换控制模板来获得这种行为,还是可以使用触发器来获得?理想情况下,我只想指定 3 个不同的资源并将它们绑定到相关属性。

Edit: Updated name "pressed" state to "pressing" to avoid confusion with possible checkbox-like behavior.

编辑:将名称“按下”状态更新为“按下”以避免与可能的类似复选框的行为混淆。

回答by Clemens

Why not use a ToggleButton with IsThreeStateset to true?

为什么不使用IsThreeState设置为的 ToggleButton true

<ToggleButton IsThreeState="True">
    <ToggleButton.Style>
        <Style TargetType="ToggleButton">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="{StaticResource TbCheckedContent}"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="False">
                    <Setter Property="Content" Value="{StaticResource TbUncheckedContent}"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="{x:Null}">
                    <Setter Property="Content" Value="{StaticResource TbNullContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>


UPDATEI guess i haven't read your question carefully enough. You may of course do something similar to the above for a Button's IsPressedand IsEnabledproperties:

更新我想我还没有足够仔细地阅读你的问题。您当然可以对 Button 的IsPressedIsEnabled属性执行与上述类似的操作:

<Button>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Content" Value="{StaticResource ButtonPressedContent}"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="False">
                    <Setter Property="Content" Value="{StaticResource ButtonNormalContent}"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Content" Value="{StaticResource ButtonDisabledContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>


You may however take a look into VisualStatesand then into Button Styles and Templatesto get an overview of how to visualize these states in a Button's ControlTemplate.

但是,您可以先查看VisualStates,然后查看Button Styles and Templates,以大致了解如何在 Button 的 ControlTemplate 中可视化这些状态。