ComboBox不会更改显示的项目

时间:2014-04-15 21:28:47

标签: c# xaml

所以我按照本教程Link这是一个允许spaces在组合框中的教程,其中ItemsSource是Enum的列表。该教程工作得很好并且解释得非常好。我目前遇到的问题,并且想知道是否有人可以提供帮助,当我选择显示时,可以说Software Engineer,然后决定它应该是Team Lead它不会改变。它仍然是软件工程师,我想知道如何解决这个问题?

namespace CaliburnMicroDemo1
{
public class EnumHelper : DependencyObject
{
    public static Type GetEnum(DependencyObject obj)
    {
        return (Type)obj.GetValue(EnumProperty);
    }

    public static void SetEnum(DependencyObject obj, string value)
    {
        obj.SetValue(EnumProperty, value);
    }

    // Using a DependencyProperty as the backing store for Enum.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnumProperty =
        DependencyProperty.RegisterAttached("Enum", typeof(Type), typeof(EnumHelper), new PropertyMetadata(null, OnEnumChanged));

    private static void OnEnumChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var control = sender as ItemsControl;

        if (control != null)
        {
            if (e.NewValue != null)
            {
                var _enum = Enum.GetValues(e.NewValue as Type);
                control.ItemsSource = _enum;
            }
        }
    }

    public static bool GetMoreDetails(DependencyObject obj)
    {
        return (bool)obj.GetValue(MoreDetailsProperty);
    }

    public static void SetMoreDetails(DependencyObject obj, bool value)
    {
        obj.SetValue(MoreDetailsProperty, value);
    }

    // Using a DependencyProperty as the backing store for MoreDetails.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MoreDetailsProperty =
        DependencyProperty.RegisterAttached("MoreDetails", typeof(bool), typeof(EnumHelper), new PropertyMetadata(false, OnMoreDetailsChanged));

    private static void OnMoreDetailsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var control = sender as FrameworkElement;
        if (control != null)
        {
            var enumobject = control.DataContext;
            var fieldInfo = enumobject.GetType().GetField(enumobject.ToString());

            var array = fieldInfo.GetCustomAttributes(false);

            if (array.Length == 0)
            {
                if (control is TextBlock)
                {
                    ((TextBlock)control).Text = enumobject.ToString();
                }
                else if (control is ContentControl)
                {
                    ((ContentControl)control).Content = enumobject;
                }
                return;
            }

            foreach (var o in array)
            {
                if (o is DescriptionAttribute)
                {
                    control.ToolTip = ((DescriptionAttribute) o).Description;
                }
                else if (o is DisplayAttribute)
                {
                    if (control is TextBlock)
                    {
                        ((TextBlock) control).Text = ((DisplayAttribute) o).Name;
                    }
                    else if (control is ContentControl)
                    {
                        ((ContentControl)control).Content = ((DisplayAttribute)o).Name;
                    }
                }
            }
        }
    }
}
}


The Enum:

public enum Designation
{
    [Display(Name="Software Engineer")]
    [Description("Software engineer responsible for core development.")]
    SoftwareEngineer,
    [Display(Name = "Team Lead")]
    [Description("Team lead responsible for leading a small team of 5 to 10 members.")]
    TeamLead,
    [Display(Name = "Product Manager")]
    [Description("Product manager responsible for core management.")]
    ProductManager
}  

0 个答案:

没有答案