WPF DataTrigger绑定的ToggleButton不允许检查 [英] WPF DataTrigger-bound ToggleButton not allowing checking

查看:275
本文介绍了WPF DataTrigger绑定的ToggleButton不允许检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让几个ToggleButton行为类似于单选按钮 - 但重要的区别是既未检查是有效的情况(仅最多一次可以检查一个,它们是互斥的。)



这是一些几乎可以工作的XAML:



I'm trying to accomplish having a couple of ToggleButtons act sort of like radio buttons--but with the important difference being that having neither checked is a valid case (only up to one can be checked at a time, they are mutually-exclusive).

Here is some XAML that almost works:

<Window>
    <Grid>
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar>
                <ToggleButton x:Name="ShowLineGridToggleButton">
                    <ToggleButton.Style>
                        <Style TargetType="ToggleButton">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsChecked, ElementName=ShowDotGridToggleButton}" Value="True">
                                    <Setter Property="IsChecked" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                    <Image Source="../Images/ShowLineGrid.png" />
                </ToggleButton>
                <ToggleButton x:Name="ShowDotGridToggleButton">
                    <ToggleButton.Style>
                        <Style TargetType="ToggleButton">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsChecked, ElementName=ShowLineGridToggleButton}" Value="True">
                                    <Setter Property="IsChecked" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                    <Image Source="../Images/ShowDotGrid.png" />
                </ToggleButton>
            </ToolBar>
        </ToolBarTray>
    <Grid>
</Window>



单击一个按钮确实会取消选中另一个按钮。不幸的是,发生的事情是点击的按钮没有被检查。鼠标移动时背景会正常变化,但当鼠标移开时,按钮显示为未选中。



注释掉Setters允许点击按钮被检查。就像Setter也取消选中点击的按钮一样。



我宁愿在XAML中处理这个问题而不是通过事件处理程序来实现代码。这会将所有内容都定义在一个地方。



想法?



TIA!


Clicking on one button will indeed uncheck the other. Unfortunately what is also happening is that the button clicked does not get checked. The background changes properly when the mouse is moved over it, but when the mouse moves away the button shows as not selected.

Commenting-out the Setters allows the clicked button to be checked. It's as if the Setter is also unchecking the clicked button.

I would rather handle this in XAML than have to resort to implementing in code via event handlers; this would keep everything defined in one place.

Ideas?

TIA!

推荐答案

你好,



你可以使用带有ToggleButton模板的RadioButton:



Hi,

You can just use RadioButton with ToggleButton Template:

<Window x:Class="WpfApplication6.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>
        <Style TargetType="RadioButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" >
                         <ContentPresenter HorizontalAlignment="Center"

                                          VerticalAlignment="Center"/>
                        </ToggleButton>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <RadioButton Content="One"/>
        <RadioButton Content="Two"/>
        <RadioButton Content="Three"/>
        <RadioButton Content="Four"/>
    </StackPanel>
</Window>


这篇关于WPF DataTrigger绑定的ToggleButton不允许检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆