2013-09-25 69 views
0

你能告訴我爲什麼這段代碼不工作嗎?如何最好地顯示此嵌套集合? (你能告訴我爲什麼我的代碼不工作嗎?)

我有一個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,但它不是,它的依賴),但其他集合不是。這可能是一個問題嗎?

+1

我幾乎完全複製了你的代碼(我已經刪除了通用的東西並且生成了我自己的數據),它起作用了! – meilke

+0

到目前爲止,這些解決方案都沒有奏效。我不知道泛型是否會導致問題?但我需要通用的。 –

回答

0

答案是我的SearchResults類沒有正確連接。我使它成爲一個具有依賴屬性的依賴對象,並且它工作正常。如果它是INotifyPropertyChanged,我假設它會進行類似的翻譯。感謝您的回覆。

1

我會創建數據模板作爲資源並在XAML中的其他地方引用它們。例如:

<Window ....> 

    <Window.Resources> 
     <DataTemplate x:Key="SearchResultTemplate" TargetType="SearchResults"> 
      <TextBlock Text={Binding PropertyValue}" 
     </DataTemplate> 

     <DataTemplate x:Key="ViewModelTemplate" TartgetType="ViewModel"> 
      <StackPanel> 
       <TextBlock Text={Binding Result.Id}/> 
       <StackPanel Orientation="Horizontal"> 
        <ItemsControl ItemsSource={Binding ResultProperties} ItemTemplate="{StaticResource SearchResultTemplate}" /> 
       </StackPanel> 
      </StackPanel 
     </DataTemplate> 

    </Window.Resources> 

    <StackPanel> 
     <ItemsControl ItemsSource={Binding SearchResults} ItemTemplate="{StaticResource ViewModelTemplate}" /> 
</Window> 

我不知道,但我認爲你正在使用是在告訴XAML解析器綁定查找名爲ResultPropertiesPropertyValue視圖模型類的屬性。 XAML解析器沒有看到它們是綁定到該集合實例的對象的屬性。像這樣拆分它應該清楚地表明屬性屬於模板應用到的實例。

+0

我試過了,它沒有破壞任何東西,但我得到了和以前一樣的結果。 –

1

你的代碼在某種程度上是正確的,但根據你想要的,它有一個流程。 StackPanel必須是ItemsPanel。像這樣改變它:

<StackPanel> 
     <ItemsControl ItemsSource="{Binding SearchResults}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Result.Id}"/> 
         <ItemsControl ItemsSource="{Binding ResultProperties}"> 
          <ItemsControl.ItemsPanel> 
           <ItemsPanelTemplate> 
            <StackPanel Orientation="Horizontal" IsItemsHost="True"/> 
           </ItemsPanelTemplate> 
          </ItemsControl.ItemsPanel> 
          <ItemsControl.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding PropertyValue}" /> 
           </DataTemplate> 
          </ItemsControl.ItemTemplate> 
         </ItemsControl> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
     <TextBlock Text="PLACEHOLDER /////"/> 
    </StackPanel> 
+0

這給我帶來了同樣的結果。我要更新我原來的帖子,並嘗試添加一些可能很重要的更多細節。 –

相關問題