繁体   English   中英

C#WPF-数据绑定单选按钮不起作用

[英]C# WPF - DataBinding RadioButtons not working

我已经搜索了多个教程并尝试了每个选项,但是我无法绑定单选按钮。 当我尝试编译以下代码时,出现错误

资源“ nullableBooleanConverter”无法解析

这是我目前在XAML中拥有的东西:

<RadioButton GroupName="grp_Option_1" Content="Yes" IsChecked="{Binding Path=OpstionSelected, Mode=TwoWay, Converter={StaticResource nullableBooleanConverter}, ConverterParameter=true}" />
<RadioButton GroupName="grp_Option_2" Content="No" IsChecked="{Binding Path=OptionSelected, Mode=TwoWay, Converter={StaticResource nullableBooleanConverter}, ConverterParameter=false}" />

我的CS有

public bool OptionSelected
{
  get { return optionSelected; }
  set
  {
    optionSelected = value;
    this.OnPropertyChanged("OptionSelected");
  }
}

public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged(string propName)
{
  if (this.PropertyChanged != null)
    this.PropertyChanged(
    this, new PropertyChangedEventArgs(propName));
}

这是我的转换器:

[ValueConversion(typeof(bool?), typeof(bool))]
public class SuccessConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        if (value == null)
        {
            return false;
        }
        else
        {
            return !((bool)value ^ param);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        return !((bool)value ^ param);
    }
}

任何帮助是极大的赞赏!

尝试在<Window.Resources>添加以下行

<Window x:Class="WpfApplication2.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" 
            xmlns:Converters="clr-namespace: here.yournamespace.converts">
    <Window.Resources>
        <Converters:SuccessConverter x:Key="nullableBooleanConverter" />
    </Window.Resources>
    <Grid>

    </Grid>
</Window>

暂无
暂无

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

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