Bind flag enums to a control and back to the enum property
I've a telerik RadAutoCompleteBox to show / select enum flags and a converter for the binding. But it works only to bind to the target not back to the property. The ConvertBack method just isn't called.
WPF:
<telerik:RadAutoCompleteBox x:Name="RadAutoCompleteBox" FilteringBehavior="{StaticResource EmptyTextFilteringBehavior}" ItemsSource="{Binding Source={local:EnumBindingSource {x:Type model:FlagEnum}}}" SelectedItems="{Binding Entity.FlagEnum, Mode=TwoWay, Converter={StaticResource ListToFlagEnumConverter}}" />
Converter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
Type type = value.GetType();
if (typeof(Enum).IsInstanceOfType(value))
{
string concatenatedEnum = ((Enum)value).ToString();
ObservableCollection<Enum> enumList = new ObservableCollection<Enum>();
foreach (string item in concatenatedEnum.Split(','))
{
enumList.Add((Enum)Enum.Parse(type, item));
}
return enumList;
}
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
ObservableCollection<Enum> enumList = (ObservableCollection<Enum>)value;
string enumString = String.Join<object>(",", enumList);
return Enum.Parse(targetType, enumString);
}
Edit: What i tried so far
Use SelectedItem[TowWay] and SelectedItems[OneWay]: Now ConvertBack gets called, but no lists will be excepted and the enum input isn't showed properly.
SelectedItem[TowWay] and SelectedItems[TowWay]: ConvertBack gets called and failed (conversion exception is thrown but target type is the right type).
I solved it with a Blend Behavior. All the converter logic is done there and i have more possibilities.
<i:Interaction.Behaviors>
<behavior:EnumFlagsBehavior EnumValue="{Binding CommunicationSystemEntity.TlsVersion, Mode=TwoWay}" NoneValue="{x:Static model:TlsVersion.None}" AllValue="{x:Static model:TlsVersion.All}" />
</i:Interaction.Behaviors>
链接地址: http://www.djcxy.com/p/44682.html
上一篇: 绑定到UWP ComboBox时无法设置ViewModel属性
下一篇: 将标志枚举绑定到控件并返回枚举属性