4

キーを列挙型として、値をカスタム オブジェクトとして持つカスタム ディクショナリがあります。このオブジェクトを xaml にバインドする必要があります。どうすればそれを行うことができますか?

私がやりたいことは、

<Button Content="{Binding ButtonGroups[my enum value].Text}"></Button>

私が試したこと、

<Button Content="{Binding ButtonGroups[local:MyEnum.Report].Text}"></Button>

<Button Content="{Binding ButtonGroups[x:Static local:MyEnum.Report].Text}">
</Button>

<Button Content="{Binding ButtonGroups[{x:Static local:MyEnum.Report}].Text}">
</Button>

しかし、上記のいずれもうまくいきません。次のコードは列挙値を表示しています。

<Button Content="{x:Static local:MyEnum.Report}"></Button>

列挙型ファイル、

public enum MyEnum
{
    Home,
    Report
}

私の辞書、

IDictionary<MyEnum, Button> ButtonGroups
4

1 に答える 1

4

Enum値のみを使用する必要があります. しかし、プロパティをButton持っていないので、私は使用しましたTextContent

 <Button Content="{Binding ButtonGroups[Home].Content}">

テスト例:

Xaml:

<Window x:Class="WpfApplication13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" x:Name="UI" Width="294" Height="79" >

    <Grid DataContext="{Binding ElementName=UI}">
         <Button Content="{Binding ButtonGroups[Home].Content}" />
    </Grid>
</Window>

コード:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();

        ButtonGroups.Add(MyEnum.Home, new Button { Content = "Hello" });
        NotifyPropertyChanged("ButtonGroups");
    }

    private Dictionary<MyEnum, Button> _buttonGroups = new Dictionary<MyEnum, Button>();
    public Dictionary<MyEnum, Button> ButtonGroups
    {
        get { return _buttonGroups; }
        set { _buttonGroups = value; }
    }

    public enum MyEnum
    {
        Home,
        Report
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

結果:

ここに画像の説明を入力

于 2013-03-05T10:22:24.767 に答える