C#WPF - DataBinding RadioButtons不工作

问题描述 投票:0回答:1

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

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

任何帮助是极大的赞赏!

c# wpf xaml data-binding
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.