简体   繁体   English

WPF ToggleButton绑定不起作用

[英]WPF ToggleButton Binding Not Working

I have two ToggleButtons; 我有两个ToggleButtons; I'm trying to make them behave like a pair of radio buttons by binding them to booleans, but it's not working. 我试图通过将它们绑定到布尔值来使它们像一对单选按钮一样工作,但是它不起作用。 Here's what I have so far: 这是我到目前为止的内容:

<ToggleButton Name="YesButton" Margin="5,0" Width="100" IsChecked="{Binding YesBool, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">Yes!</ToggleButton>

<ToggleButton Name="NoButton" Margin="5,0" Width="100" IsChecked="{Binding NoBool, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">No!</ToggleButton>

And

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
    }
}

public class Thingy : INotifyPropertyChanged
{
    private bool _yesno;

    public bool YesBool
    {
        get { return _yesno; }
        set { _yesno = value; NotifyPropertyChanged("YesBool"); }
    }

    public bool NoBool
    {
        get { return !_yesno; }
        set { _yesno = !value; NotifyPropertyChanged("NoBool"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

As far as I can tell, everyone else who had this problem misspelled their binding or didn't use NotifyPropertyChanged, but (as far as I can tell) I'm doing both of those things. 据我所知,遇到此问题的其他所有人都拼写错误或未使用NotifyPropertyChanged,但是(据我所知)我正在做这两种事情。 What am I doing wrong? 我究竟做错了什么?

将您的xaml中的DataContext设置为Thingy类,而不是“ this”窗口。

You're question doesn't specify if the booleans are needed or are only there to help you get the wanted behaviour. 您的问题是未指定布尔值是必需的还是仅用于帮助您获得所需的行为。

So in case you don't need them, you could also go for a function, which unchecks the other button. 因此,如果您不需要它们,也可以使用某个功能,取消选中另一个按钮。 This could be also used for more than 2 ToggleButtons. 这也可以用于2个以上的ToggleButton。
If you can be certain there are no other coontrols than ToggleButtons, you could also go for a foreach loop without the type-check. 如果可以确定除ToggleButtons之外没有其他同类,那么也可以不进行类型检查就进行foreach循环。

public void ToggleButtonChecked(object sender, RoutedEventArgs e)
    {
        ToggleButton btn = sender as ToggleButton;
        if (btn == null)
            return;
        Panel container = btn.Parent as Panel;
        if (container == null)
            return;

        for (int i = 0; i<container.Children.Count; i++)
        {
            if (container.Children[i].GetType() == typeof(ToggleButton))
            {
                ToggleButton item = (ToggleButton)container.Children[i];
                if (item != btn && item.IsChecked == true)
                    item.IsChecked = false;
            }
        }
    }

XAML XAML

<ToggleButton x:Name="tb1" Checked="ToggleButtonChecked"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM