0
我有兩個ComboBox
。第一個來源是dictionary
,字符串作爲鍵,對象作爲值。選擇一個項目後,第二個ComboBox
將填充所選項目的獨立dictionary
中的密鑰。當選擇第二個ComboBox
中的項目時,TextBlock
應顯示在第二個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:
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
,但值沒有被正確設置。
不幸試過了,仍然一無所獲。我在文本塊中添加了一個轉換器,以便我可以調試該值,並且該值顯示爲空。所以它不會因爲某些原因而拿起價值。 – pfinferno
您是否看到ModelList組合框中的項目? – mm8
是的。第一個ComboBox顯示GPHDT,當我選擇它時,第二個顯示「MessageID」。儘管如此,文本塊保持空白,而不是顯示MessageID屬性等於的「3」。 – pfinferno