0

コンボボックスへの列挙型を使用した単純な双方向バインディングを実行しようとしていますが、これまでのところ私のコードで動作するものは見つかりませんでした。

私の列挙型(C#):

public enum CurrenciesEnum { USD, JPY, HKD, EUR, AUD, NZD };

Enum が設定する必要があるプロパティ / バインド先:

    private string _ccy;
    public string Ccy
    {
        get
        {
            return this._ccy;
        }
        set
        {
            if (value != this._ccy)
            {
                this._ccy= value;
                NotifyPropertyChanged("Ccy");
            }
        }
    }

動作しない Xaml コード:

    <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ObjectDataProvider x:Key="Currencies" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="ConfigManager:CurrenciesEnum" />
                </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </ResourceDictionary>
</UserControl.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource Currencies}}" SelectedItem="{Binding Ccy, Mode=TwoWay}"/>

よろしくお願いします。

4

2 に答える 2

0

問題は、 aEnumを aにバインドしていることです。これは、バインド エンジンstringの既定の操作により、一方向にしか機能しません。ToString

値のみを使用している場合は、メソッド名をこれにstring変更すると、文字列値が返され、両方の方法でバインドされます。他のオプションは、文字列ではなく型にバインドすることです。ObjectDataProviderGetNamesEnumEnum

    <ObjectDataProvider x:Key="Currencies" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="ConfigManager:CurrenciesEnum" />
            </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
于 2013-03-28T10:30:44.110 に答える
0

列挙型をディクショナリにロードします

public static Dictionary<T, string> EnumToDictionary<T>()
        where T : struct
{
    Type enumType = typeof(T);

    // Can't use generic type constraints on value types,
    // so have to do check like this
    if (enumType.BaseType != typeof(Enum))
        throw new ArgumentException("T must be of type System.Enum");

    Dictionary<T, string> enumDL = new Dictionary<T, string>();
    //foreach (byte i in Enum.GetValues(enumType))
    //{
    //    enumDL.Add((T)Enum.ToObject(enumType, i), Enum.GetName(enumType, i));
    //}
    foreach (T val in Enum.GetValues(enumType))
    {
        enumDL.Add(val, val.ToString());
    }
    return enumDL;
}
于 2013-03-28T13:26:45.147 に答える