我試圖創建一個具有擴展的列表視圖中的兩個不同的項目來源,我希望當我按下擴大他們中的一個,從不同的項目源名稱的列表被載入。到目前爲止,我得到的是,當我設置的項目源LineRouteCollection設置擴展的頭部,即使我綁定擴展顯示來自AllStopsCollection結合「名」的名稱顯示從LineRouteCollection源的名稱,而不是名稱來自AllStopsCollection,因爲我想要的。你能看看我的代碼,並告訴我我做錯了什麼嗎?Catel MVVM如何設置一個列表框
<ListBox>
<ItemsControl ItemsSource="{Binding LineRouteCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Name}" MinHeight="70">
<ListBox >
<ItemsControl ItemsSource ="{Binding AllStopsCollection}">
<TextBlock Text="{Binding Name}"></TextBlock>
</ItemsControl>
</ListBox>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ListBox>
編輯:
在我的視圖模型我加載觀察集合(AllStopsCollection & LineRouteCollection)是這樣的:
private ObservableCollection<Route> AllLineRoutes;
private ObservableCollection<StopView> AllRouteStops;
//List of all Routes connected to the selected line
public ObservableCollection<Route> LineRouteCollection // Must be property or DP to be bound!
{
get { return AllLineRoutes; }
set
{
if (Equals(value, AllLineRoutes)) return;
AllLineRoutes = value;
}
}
//List of all stops
public ObservableCollection<StopView> AllStopsCollection // Must be property or DP to be bound!
{
get { return AllRouteStops; }
set
{
if (Equals(value, AllRouteStops)) return;
AllRouteStops = value;
}
}
而且我填的數據集合中的類的構造函數。我正確加載數據,我可以看到它,但它只是在擴展器展開後纔會出現在文本框中。
這裏的問題是您需要提供'ItemTemplate'而不是嵌套文本框而不是'ItemsControl'。你應該有一個與ItemsControl內部的ItemsControl中的第一個'ItemsControl'相同的設置,其中包含文本塊的ItemsControl.ItemTemplate',否則文本塊的綁定範圍就是第一個'DataTemplate'的綁定範圍 – Charleh 2014-09-05 12:01:25
我已經更新了我的答案 - 你的問題是綁定範圍(我希望你有一個模型數據的層次結構而不是兩個平面集合) – Charleh 2014-09-08 11:12:02