C# C WPF-数据绑定单选按钮不工作
我已经搜索了几个教程并尝试了每个选项,但我无法绑定我的单选按钮。当我试图编译下面的代码时,我得到了错误 无法解析资源nullableBooleanConverter 以下是我目前在XAML中拥有的内容:C# C WPF-数据绑定单选按钮不工作,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我已经搜索了几个教程并尝试了每个选项,但我无法绑定我的单选按钮。当我试图编译下面的代码时,我得到了错误 无法解析资源nullableBooleanConverter 以下是我目前在XAML中拥有的内容: <RadioButton GroupName="grp_Option_1" Content="Yes" IsChecked="{Binding Path=OpstionSelected, Mode=TwoWay, Converter={StaticResource nullableBool
<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}" />
这是我的转换器:
[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);
}
}
非常感谢您的帮助 尝试将以下行添加到
不幸的是,这没有起作用。我收到一个错误:URI clr名称空间:WPF1.converts不是有效的名称空间标识符。能否将名称空间发布到定义SuccessConverter的位置?我根据您的答案找到了它。我的代码在使用xmlns:Converters=clr namespace:WPF1时工作,其中WPF1是我的命名空间。我必须把它放在app.xaml部分,并且必须放在application.resources部分下。
[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 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>