2017-04-26 13 views
0

私は2つのComboBoxを持っています。最初のソースはdictionaryで、文字列はキー、オブジェクトは値です。アイテムを選択すると、2番目のComboBoxには、選択したアイテムの別のdictionaryのキーが入力されます。 2番目のComboBoxの項目が選択されている場合、TextBlockには、2番目のComboBoxで選択されたキーの値が表示されます。ただし、テキストブロックは常に空に見えます。私は、その価値が実際のデータを持っていることを確かめました。それは、それが拘束力のある問題だと私につながります。ネストされた辞書をソースとしてコンボボックスにテキストブロックをバインドする方法

GPHDTModel gphdtModel = new GPHDTModel(); 

private Dictionary<string, object> models = new Dictionary<string, object>(); 
public Dictionary<string, object> Models 
{ 
    get 
    { 
     return models; 
    } 
} 

public MainWindowViewModel() 
{ 
    gphdtModel.MessageID = "3"; 
    models.Add("GPHDT", gphdtModel); 
} 

次はここにGPHDTModelです:

は、ここに私のViewModelの関連セクションだ

private Dictionary<string, string> _fields = new Dictionary<string, string>(); 
public Dictionary<string, string> Fields 
{ 
    get 
    { 
     return _fields; 
    } 
} 

public GPHDTModel() 
{ 
    _fields.Add("MessageID", MessageID); 
} 

private string _messageID; 
public string MessageID 
{ 
    get { return _messageID; } 
    set { _messageID = value; OnPropertyChanged("MessageID"); } 
} 

最後にビュー:

<ItemsControl ItemsSource="{Binding DataModelCollection}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <ComboBox x:Name="NMEAlist" 
          DisplayMemberPath="Key" 
          ItemsSource="{Binding Path=DataContext.Models, 
               RelativeSource={RelativeSource Mode=FindAncestor, 
                       AncestorType={x:Type ItemsControl}}}" 
          SelectedValuePath="Value" /> 
       <ComboBox x:Name="ModelList" 
          DisplayMemberPath="Key" 
          ItemsSource="{Binding SelectedItem.Value.Fields, 
               ElementName=NMEAlist}" 
          SelectedValuePath="Value" /> 
       <TextBlock Text="{Binding Value, 
              ElementName=ModelList}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

編集:バインディングでコンバータを使用しますTextBlockがデバッグするには、正しいキー(この場合は "MessageID")が表示されています。キーの値は "3"でなければなりません。

@ mm8は、以下のようにテキストブロックをバインドする場合、Text="{Binding SelectedItem.Key, ElementName=ModelList}"とします。「MessageID」がテキストブロックに表示されます。したがって、バインディングはSelectedItem.Valueを使用して正しいですが、値が正しく設定されていません。

答えて

1

ComboBoxSelectedItem財産のValueプロパティにバインドしよう:

これを試し
<TextBlock Text="{Binding SelectedItem.Value, ElementName=ModelList}" /> 
+0

、まだ残念ながら何もありません。私は値をデバッグできるように、テキストブロックにコンバーターを追加し、値はnullとして表示されます。だから何らかの理由で価値を拾わない。 – pfinferno

+0

ModelList ComboBoxの項目が表示されますか? – mm8

+0

はい。最初のComboBoxはGPHDTを表示していて、それを選択すると2番目のComboBoxは "MessageID"を表示します。しかし、それを選択すると、TextIDプロパティは "3"を表示するのではなく、空のままです。 – pfinferno

関連する問題