3

以下のコードでは、コンボボックスの ItemsSource と SelectedValue をモデルの 2 つの異なるプロパティにバインドしようとしています。TickerList は Dictionary[string, string>. リストは正常に表示され、選択すると SelectedValue が正しく入力されます。ただし、ViewModel.SelectedTicker プロパティをディクショナリの値の 1 つに設定すると、ComboBox で選択された項目は変更されません。私はこのタイプのバインディングに感銘を受けました。モデルに値を設定すると、コントロールはその変更を反映します。私が間違っているかもしれないことについて何か考えはありますか?(注: 意図的に辞書キーを表示し、辞書の値を selectedValePath として使用しています。これは、viewmodel.SelectedTicker プロパティに設定した値でもあります。これは設計によるものです)。

        //bind to list of TickerList
        Binding listBinding = new Binding() { Source = ViewModel.TickerList };
        cbPriceSource.DisplayMemberPath = "Key";
        cbPriceSource.SelectedValuePath = "Value";
        cbPriceSource.SetBinding(ComboBox.ItemsSourceProperty, listBinding);

        //bind selection to ViewModel.SelectedTicker
        cbPriceSource.DataContext = ViewModel;
        Binding selectedItemBinding = new Binding() { Source = ViewModel, Path = new PropertyPath("SelectedTicker"), Mode = BindingMode.TwoWay };
        cbPriceSource.SetBinding(ComboBox.SelectedValueProperty, selectedItemBinding);
4

0 に答える 0