【发布时间】:2026-02-12 16:20:04
【问题描述】:
我有一个单选按钮组,我绑定到一个布尔值,但它没有在 XAML 中被拾取 - 我确信它很简单 - 任何指针都值得赞赏。
passed 设置为 false。
XAML
<RadioButton Width="64"
IsChecked="{Binding passed, Converter={StaticResource BoolInverterConverter}}"
GroupName="Result">Yes</RadioButton>
<RadioButton Width="64"
IsChecked="{Binding passed, Converter={StaticResource BoolInverterConverter}}"
GroupName="Result">No</RadioButton>
BoolInverterConverter:
public class BoolInverterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
}
未填充:
视图模型:-
public ResultsViewModel()
{
private Results_results = new Results();
public ResultsViewModel()
{
_results.Passed= false;
}
}
结果类:-
public class Results
{
private bool passed;
public bool Passed{ get => passed; set => passed= value; }
}
【问题讨论】:
-
你能发布你的 ViewModel 吗?您还可以在调试时检查 Visual Studio 输出选项卡,它会告诉您是否找不到绑定。
-
@Seb - 添加了 ViewModel - 调试未显示绑定失败...
-
删除您的一个转换器。大概是的。你把两者都颠倒了。这不可能。
标签: c# wpf data-binding binding ivalueconverter