第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將枚舉屬性數(shù)據(jù)庫為WPF中的組合框

將枚舉屬性數(shù)據(jù)庫為WPF中的組合框

慕村225694 2019-07-22 15:27:20
將枚舉屬性數(shù)據(jù)庫為WPF中的組合框例如,以下代碼為例:public enum ExampleEnum { FooBar, BarFoo }public class ExampleClass : INotifyPropertyChanged{     private ExampleEnum example;     public ExampleEnum ExampleProperty      { get { return example; } { /* set and notify */; } }}我希望將屬性ExampleProperty數(shù)據(jù)庫綁定到ComboBox,這樣它就可以顯示“fooBar”和“barfoo”選項,并在雙模式下工作。最理想的是,我希望我的ComboBox定義如下所示:<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />目前,我在我的窗口中安裝了ComboBox.electionChanged和ExampleClass.PropertyChanged事件的處理程序,我在窗口中手動進行綁定。有什么更好的或者某種規(guī)范的方法嗎?您是否通常使用轉(zhuǎn)換器,以及如何使用正確的值填充ComboBox?我現(xiàn)在甚至不想開始使用i18n。編輯因此,有一個問題得到了回答:如何用正確的值填充ComboBox。從靜態(tài)Enum.GetValue方法中通過ObjectDataProvider將Enum值檢索為字符串列表:<Window.Resources>     <ObjectDataProvider MethodName="GetValues"         ObjectType="{x:Type sys:Enum}"         x:Key="ExampleEnumValues">         <ObjectDataProvider.MethodParameters>             <x:Type TypeName="ExampleEnum" />         </ObjectDataProvider.MethodParameters>     </ObjectDataProvider></Window.Resources>我可以將它用作ComboBox的ItemsSource:<ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/>
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻1827條經(jīng)驗 獲得超9個贊

可以創(chuàng)建自定義標(biāo)記擴展。

使用示例:

enum Status{
    [Description("Available.")]
    Available,
    [Description("Not here right now.")]
    Away,
    [Description("I don't have time right now.")]
    Busy}

在XAML的頂部:

    xmlns:my="clr-namespace:namespace_to_enumeration_extension_class

然后.。

<ComboBox 
    ItemsSource="{Binding Source={my:Enumeration {x:Type my:Status}}}" 
    DisplayMemberPath="Description" 
    SelectedValue="{Binding CurrentStatus}"  
    SelectedValuePath="Value"  />

以及實施.。

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


查看完整回答
反對 回復(fù) 2019-07-22
?
守著星空守著你

TA貢獻1799條經(jīng)驗 獲得超8個贊

在視圖模型中,您可以擁有:

    public MyEnumType SelectedMyEnumType 
    {
        get { return _selectedMyEnumType; }
        set { 
                _selectedMyEnumType = value;
                OnPropertyChanged("SelectedMyEnumType");
            }
    }

    public IEnumerable<MyEnumType> MyEnumTypeValues
    {
        get
        {
            return Enum.GetValues(typeof(MyEnumType))
                .Cast<MyEnumType>();
        }
    }

在XAML中,ItemSource綁定到MyEnumTypeValue,SelectedItem綁定到SelectedMyEnumType。

<ComboBox SelectedItem="{Binding SelectedMyEnumType}" ItemsSource="{Binding MyEnumTypeValues}"></ComboBox>


查看完整回答
反對 回復(fù) 2019-07-22
  • 3 回答
  • 0 關(guān)注
  • 395 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號