C#WPF - DataBinding RadioButtons不工作

时间:2014-01-14 17:27:12

标签: c# wpf xaml data-binding

我已经搜索了几个教程并尝试了每个选项,但我无法将我的单选按钮绑定。当我尝试编译以下代码时,我收到错误

  

无法解析资源“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);
    }
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

尝试在<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>