将标志枚举绑定到控件并返回到枚举 属性

Bind flag enums to a control and back to the enum property

我有一个 telerik RadAutoCompleteBox 来显示/select 枚举标志和一个用于绑定的转换器。但它只适用于绑定到目标而不是返回到 属性。只是没有调用 ConvertBack 方法。

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}}" />

转换器:

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);
}

编辑:到目前为止我尝试了什么

我用混合行为解决了它。所有的转换器逻辑都在那里完成,我有更多的可能性。

<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>