WPF / Caliburn微观相关问题。
我有4个单选按钮,我将IsChecked属性绑定到ArrowType,它有一个我创建的LogicArrowEnum枚举类型。
Radiobuttons使用转换器根据单击的按钮将相关枚举正确分配给ArrowType属性。
XAML:
<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);
}
}
代码运行得非常好 - 用户单击一个按钮,ArrowType属性被正确绑定。
我也想让这项工作倒退。例如,如果我通过代码将ArrowType属性设置为LogicArrowEnum.ASSIGN,则UI应显示已切换Assign按钮。出于某种原因,这不能按预期工作。在set属性方法中,每当我将ArrowType属性赋值给任意枚举时,arrowType的私有字段首先被指定为我想要的值,但是一旦代码到达NotifyOfPropertyChange方法,它就会进入再次设置set方法,然后将arrowType私有字段重置为先前切换的按钮。
这是与Caliburn Micro相关的错误还是与WPF相关的一些错误?我该如何解决这个问题?
答案 0 :(得分:1)
与Caliburn.Micro无关。 看看这个:How to bind RadioButtons to an enum?
从Scott回答一个转换器,它会正常工作。