Reputation: 7030
WPF / Caliburn micro related question.
I have 4 radio buttons that I am binding the IsChecked property to ArrowType, which has a type of LogicArrowEnum enumeration that I created.
Radiobuttons use a converter to properly assign the relevant enumeration to the ArrowType property based on which button has been clicked.
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" />
Code:
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);
}
}
The code works wonderfully - user clicks a button, the ArrowType property is properly bound.
I'd like to make this work backwards as well. For example, if I set the ArrowType property to LogicArrowEnum.ASSIGN via code, the UI should show that the Assign button has been toggled. For some reason, this does not work as expected. Inside the set method of property, whenever I assign the ArrowType property to an arbitrary enumeration, the private field of arrowType is first assigned as the value that I want it to, but then as soon as the code reaches NotifyOfPropertyChange method, it makes it enter the set method again but then resets the arrowType private field to the previously toggled button.
Is this a Caliburn Micro related bug or some WPF related bug? How can I fix this?
Upvotes: 0
Views: 1399
Reputation: 7585
There is no relation to Caliburn.Micro. Check out this: How to bind RadioButtons to an enum?
Take a converter from Scott answer and it will work fine.
Upvotes: 1