你能告訴我爲什麼這段代碼不工作嗎?如何最好地顯示此嵌套集合? (你能告訴我爲什麼我的代碼不工作嗎?)
我有一個viewable集合,它有一個observable集合,它有一個可觀察的resultproperties集合。我似乎無法顯示像我想要的結果屬性的嵌套集合。
這裏是(抽象的可讀性)對象:
class ViewModel
{
public ObservableCollection<SearchResults<TDomain>> SearchResults { get; set; }
}
class SearchResults<TDomain>
{
public TDomain Result { get; set; }
public ObservableCollection<ResultProperty> ResultProperties { get; set; }
}
class ResultProperty
{
public string PropertyValue { get; set; }
}
這裏是XAML,我不能去上班。在DataContext設置爲視圖模型:
<StackPanel>
<ItemsControl ItemsSource={Binding SearchResults}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text={Binding Result.Id}/>
<StackPanel Orientation="Horizontal">
<ItemsControl ItemsSource={Binding ResultProperties}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding PropertyValue}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="PLACEHOLDER /////"/>
</StackPanel>
我正在尋找的結果是這樣的:
[Stack panel to keep things orderly]
1
property1property2property3...
2
property1property2property3...
3
property1property2property3...
PLACEHOLDER /////
我得到的結果是這些
[Stack panel to keep things orderly]
1
2
3
PLACEHOLDER /////
。換句話說,該綁定不會拾取該字符串。我已經驗證了這些集合正在像預期那樣填充。但我無法讓xaml工作。
* *附加信息
好了,所以我嘗試了一些解決方案,但他們沒有工作,所以我要添加更多的細節,因爲也許我失去了一些東西有關如何集合正在更新。
視圖模型上有一個按鈕,所謂的「搜索」,它使用一個ICommand調用一個視圖模型的TrySearch方法是如下:
public void TrySearch()
{
var results = _model.GetAll();
foreach(var result in results)
this.SearchResults.Add(new SearchResults<TDomain>(result));
}
難道還有一個原因,這並不因爲工作收集更新的方式? SearchResults是一個依賴屬性(我知道我知道,它應該是INPC,但它不是,它的依賴),但其他集合不是。這可能是一個問題嗎?
我幾乎完全複製了你的代碼(我已經刪除了通用的東西並且生成了我自己的數據),它起作用了! – meilke
到目前爲止,這些解決方案都沒有奏效。我不知道泛型是否會導致問題?但我需要通用的。 –