【问题标题】:Bind flag enums to a control and back to the enum property将标志枚举绑定到控件并返回到枚举属性
【发布时间】:2016-09-27 08:59:22
【问题描述】:

我有一个 Telerik RadAutoCompleteBox 来显示/选择枚举标志和一个用于绑定的转换器。但它仅适用于绑定到目标而不是返回到属性。只是没有调用 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);
}

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

  • 使用 SelectedItem[TowWay] 和 SelectedItems[OneWay]:现在将调用 ConvertBack,但不会排除任何列表,并且无法正确显示枚举输入。

  • SelectedItem[TowWay] 和 SelectedItems[TowWay]:ConvertBack 被调用并失败(抛出转换异常,但目标类型是正确的类型)。

【问题讨论】:

    标签: c# wpf telerik ivalueconverter


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 2011-12-21
      • 2015-10-17
      相关资源
      最近更新 更多