SORU
12 EYLÜL 2008, Cuma


WPF ComboBox için bir numaralama özelliği veri bağlama

Örnek olarak aşağıdaki kodu atın:

public enum ExampleEnum { FooBar, BarFoo }

public class ExampleClass : INotifyPropertyChanged
{
    private ExampleEnum example;

    public ExampleEnum ExampleProperty 
    { get { return example; } { /* set and notify */; } }
}

Bir seçenekler "" ve "BarFoo" ve eserleri modunda iki yönlü. Falanca gösterecek şekilde bir ComboBox özellik ExampleProperty databind etmek istiyorum En iyi şekilde ComboBox benim tanımı bakmak istediğim bir şey bu

<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />

Şu anda ComboBox için işleyicileri var.SelectionChanged ve ExampleClass.PropertyChanged olayları bağlama elle yaptığım yer benim Penceresinde yüklü.

Daha iyi ya da standart bir yol var mı? Genellikle kullanım Dönüştürücüler ve nasıl doğru değerleri ile ComboBox doldurmak misin? Hatta i18n başlamak için şu anda istemiyorum.

Edit

Bir soruya cevap verdi: Nasıl doğru değerleri ile ComboBox doldurmak.

Statik Numaralama bir ObjectDataProvider üzerinden dizeleri listesini Numaralandırma değerleri almak.GetValues metodu:

<Window.Resources>
    <ObjectDataProvider MethodName="GetValues"
        ObjectType="{x:Type sys:Enum}"
        x:Key="ExampleEnumValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="ExampleEnum" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

Bu benim için ComboBox: bir İtemsSource olarak kullanabilirsiniz

<ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/>

CEVAP
9 Aralık 2010, PERŞEMBE


Özel biçimlendirme uzantısı oluşturabilirsiniz.

Kullanım örneği:

enum Status
{
    [Description("Available.")]
    Available,
    [Description("Not here right now.")]
    Away,
    [Description("I don't have time right now.")]
    Busy
}
<ComboBox 
    ItemsSource="{Binding Source={my:Enumeration {x:Type my:Status}}}" 
    DisplayMemberPath="Description" 
    SelectedValue="{Binding CurrentStatus}"  
    SelectedValuePath="Value"  /> 

Ve uygulanması...

public class EnumerationExtension : MarkupExtension
  {
    private Type _enumType;


    public EnumerationExtension(Type enumType)
    {
      if (enumType == null)
        throw new ArgumentNullException("enumType");

      EnumType = enumType;
    }

    public Type EnumType
    {
      get { return _enumType; }
      private set
      {
        if (_enumType == value)
          return;

        var enumType = Nullable.GetUnderlyingType(value) ?? value;

        if (enumType.IsEnum == false)
          throw new ArgumentException("Type must be an Enum.");

        _enumType = value;
      }
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
      var enumValues = Enum.GetValues(EnumType);

      return (
        from object enumValue in enumValues
        select new EnumerationMember{
          Value = enumValue,
          Description = GetDescription(enumValue)
        }).ToArray();
    }

    private string GetDescription(object enumValue)
    {
      var descriptionAttribute = EnumType
        .GetField(enumValue.ToString())
        .GetCustomAttributes(typeof (DescriptionAttribute), false)
        .FirstOrDefault() as DescriptionAttribute;


      return descriptionAttribute != null
        ? descriptionAttribute.Description
        : enumValue.ToString();
    }

    public class EnumerationMember
    {
      public string Description { get; set; }
      public object Value { get; set; }
    }
  }

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • celebrateubuntu

    celebrateubu

    23 Mayıs 2011
  • emimusic

    emimusic

    10 Mart 2006
  • Crossover

    Crossover

    18 HAZİRAN 2007