私は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
を使用して正しいですが、値が正しく設定されていません。
、まだ残念ながら何もありません。私は値をデバッグできるように、テキストブロックにコンバーターを追加し、値はnullとして表示されます。だから何らかの理由で価値を拾わない。 – pfinferno
ModelList ComboBoxの項目が表示されますか? – mm8
はい。最初のComboBoxはGPHDTを表示していて、それを選択すると2番目のComboBoxは "MessageID"を表示します。しかし、それを選択すると、TextIDプロパティは "3"を表示するのではなく、空のままです。 – pfinferno