WPF绑定单选按钮 [英] Radiobutton wpf binding

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

问题描述

我有一个主从wpf应用程序. "master"是一个datagrid,"detail"是两个单选按钮.基于行选择,将在详细信息"部分中选中单选按钮.

I have a master-detail wpf application. The "master" is a datagrid , and "detail" is two radio buttons. Based on the row selection the radio buttons are checked in the "detail" section.

我使用inttoboolean转换器按以下方式绑定我的单选"按钮. xaml:

I am binding my Radio button the following way using a inttoboolean converter. xaml :

<StackPanel Margin="2">
  <RadioButton Margin="0,0,0,5" Content="In Detail" IsChecked="{Binding Path=itemselect.OutputType, Converter ={StaticResource radtointOTSB}, ConverterParameter= 0}"/>
  <RadioButton Content="In Breif" IsChecked="{Binding Path=itemselect.OutputType, Converter ={StaticResource radtointOTSB}, ConverterParameter= 1}"/>
</StackPanel>

在视图模型中:

public class radtointOTSB : IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int OTint = Convert.ToInt32(value);
        if (OTint == int.Parse(parameter.ToString()))
            return true;
        else
            return false;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return parameter;
    }
}

对于datagrid中的前几个选择,我的实现效果很好.突然之间,我的单选按钮都没有被选中.

My implementation works well for the first few selections in datagrid. And all of a sudden , neither of my radio button is selected.

我不知道为什么会发生,欢迎任何建议.

I have no clue on why it happens, any suggestion is welcomed.

先谢谢了.

推荐答案

搜索有关绑定多个RadioButton的问题-那里有足够的投诉.基本上,绑定不会收到False的值,因为它没有传递给Dependency Property..etc等

Search for problems with Binding multiple RadioButtons - there are enough complaints out there. Basically the binding won't receive the value of False because it not being passed to the Dependency Property..etc etc

尝试使用以下类而不是常规的RadioButton绑定到IsCheckedExt,因为它会强制复选框的IsChecked值进行更新.

Try using the following class instead of the regular RadioButton, bind to IsCheckedExt, as it forces the checkbox's IsChecked value to update.

public class RadioButtonExtended : RadioButton
{
    public static readonly DependencyProperty IsCheckedExtProperty =
        DependencyProperty.Register("IsCheckedExt", typeof(bool?), typeof(RadioButtonExtended),
                                    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsCheckedRealChanged));

    private static bool _isChanging;

    public RadioButtonExtended ()
    {
        Checked += RadioButtonExtendedChecked;
        Unchecked += RadioButtonExtendedUnchecked;
    }

    public bool? IsCheckedExt
    {
        get { return (bool?)GetValue(IsCheckedExtProperty); }
        set { SetValue(IsCheckedExtProperty, value); }
    }

    public static void IsCheckedRealChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        _isChanging = true;
        ((RadioButtonExtended)d).IsChecked = (bool)e.NewValue;
        _isChanging = false;
    }

    private void RadioButtonExtendedChecked(object sender, RoutedEventArgs e)
    {
        if (!_isChanging)
            IsCheckedExt = true;
    }

    private void RadioButtonExtendedUnchecked(object sender, RoutedEventArgs e)
    {
        if (!_isChanging)
            IsCheckedExt = false;
    }
}

这篇关于WPF绑定单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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