C# 通过代码切换绑定的单选按钮
WPF/Caliburn微相关问题 我有4个单选按钮,将IsChecked属性绑定到ArrowType,它具有我创建的LogicArrowEnum枚举类型 单选按钮使用转换器根据单击的按钮将相关枚举正确分配给ArrowType属性 XAML: 代码运行得非常好-用户单击一个按钮,ArrowType属性被正确绑定 我想让这项工作倒退。例如,如果通过代码将ArrowType属性设置为LogicArrowEnum.ASSIGN,则UI应显示已切换ASSIGN按钮。由于某些原因,这并没有像预期的那样起作用。在属性的set方法中,每当我将ArrowType属性指定给任意枚举时,ArrowType的私有字段首先被指定为我希望它指定的值,但一旦代码到达NotifyOfPropertyChange方法,它使其再次进入set方法,但随后将arrowType private字段重置为先前切换的按钮C# 通过代码切换绑定的单选按钮,c#,.net,wpf,caliburn.micro,C#,.net,Wpf,Caliburn.micro,WPF/Caliburn微相关问题 我有4个单选按钮,将IsChecked属性绑定到ArrowType,它具有我创建的LogicArrowEnum枚举类型 单选按钮使用转换器根据单击的按钮将相关枚举正确分配给ArrowType属性 XAML: 代码运行得非常好-用户单击一个按钮,ArrowType属性被正确绑定 我想让这项工作倒退。例如,如果通过代码将ArrowType属性设置为LogicArrowEnum.ASSIGN,则UI应显示已切换ASSIGN按钮。由于某些原因,这并没有像预期的那样起作
这是与Caliburn Micro相关的bug还是与WPF相关的bug?如何解决此问题?与Caliburn.Micro没有关系。 看看这个:
从Scott answer获取一个转换器,它可以正常工作。我尝试了这个方法,但遇到了相同的问题。你绝对确定这和Caliburn Micro没有关系吗?由于NotifyOfPropertyChange,属性的setter激活了两次。啊,没关系,我只是注意到您提到的名称与公认的应答者不同。谢谢
<Window.Resources>
<my:EnumToBoolConverter x:Key="EBConverter"/>
</Window.Resources>
...
<RadioButton IsChecked="{Binding ArrowType,
Converter={StaticResource EBConverter},
ConverterParameter={x:Static my:LogicArrowEnum.ARROW}}"
Name="LogicArrow"
Style="{StaticResource {x:Type ToggleButton}}"
Width="50"
<TextBlock Text="Arrow"/>
</RadioButton>
<RadioButton IsChecked="{Binding ArrowType,
Converter={StaticResource EBConverter},
ConverterParameter={x:Static my:LogicArrowEnum.ASSIGN}}"
Name="LogicAssign"
Style="{StaticResource {x:Type ToggleButton}}"
Width="50"
<TextBlock Text="Assign"/>
</RadioButton>
<RadioButton
IsChecked="{Binding ArrowType,
Converter={StaticResource EBConverter},
ConverterParameter={x:Static my:LogicArrowEnum.IF}}"
Name="LogicIf"
Style="{StaticResource {x:Type ToggleButton}}"
Width="50"
<TextBlock Text="If" />
public class EnumToBoolConverter : IValueConverter
{
public object Convert(object value,
Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (parameter.Equals(value))
return true;
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return parameter;
}
}
public enum LogicArrowEnum
{
ARROW = 1,
ASSIGN = 2,
IF = 3,
IF_ELSE = 4
}
public LogicArrowEnum ArrowType
{
get { return arrowType; }
set
{
arrowType = value;
NotifyOfPropertyChange(() => ArrowType);
}
}