2011-11-28 59 views
2

我有一個描述某項任務的類。這個「任務」類有一個子步驟列表。如何顯示主詳細信息在一個可摺疊的ListView

我想要做的是顯示在ListView這些信息在任務描述應該被用作頁眉和分步驟的細節

我也希望能夠摺疊和展開這些羣體。

這裏是我試過到目前爲止(簡化,但你會得到希望的想法):

public class Task : INotifyPropertyChanged 
{ 
    public string Description; 
    public ObservableCollection<String> substeps; 
    ... 
} 
在視圖模型

Task t = new Task(); 
t.Description = "Task1"; 
t.substeps.Add("substep 1"); 
t.substeps.Add("substep 2"); 
... 

Tasks = new CollectionViewSource { Source = TaskList }; //TaskList is just a ObservableCollection<Task> 
Tasks.GroupDescriptions.Add(new PropertyGroupDescription("Description")); 

在XAML:

<s:SurfaceListBox Width="500" Height="1000" ItemsSource="{Binding TaskList.View}"> 
       <s:SurfaceListBox.Resources> 
        <Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate> 
            <Expander Header="{Binding Description}" IsExpanded="True"> 
             <ItemsPresenter/> 
            </Expander> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </s:SurfaceListBox.Resources> 
       <s:SurfaceListBox.GroupStyle> 
        <GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/> 
       </s:SurfaceListBox.GroupStyle> 
       <s:SurfaceListBox.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding substeps}"/> 
        </DataTemplate> 
       </s:SurfaceListBox.ItemTemplate> 
      </s:SurfaceListBox> 

結果是我的摺疊項列表框。如果我展開它,而不是每一步我只看到「(上市)」

我一定要建立在描述與子步驟一類,並在它的描述,和組嗎?

回答

1

試試這個:

<s:SurfaceListBox.ItemTemplate> 
    <DataTemplate> 
     <ItemsControl ItemsSource={Binding substeps} /> 
    </DataTemplate> 
</s:SurfaceListBox.ItemTemplate> 

你的問題是,字符串綁定到ObservableCollection只會在該集合做ToString()。您需要遍歷集合並顯示每個項目。通過使用ItemsControl因爲我已經做了,你也可以DataTemplate每個子任務,你認爲合適。

的ControlTemplate綁定

Expander頭綁定不會工作,因爲它是一個ControlTemplate這是Style內部內。該DataContext的控制模板不會是你ViewModel但控制(即SurfaceListBox)本身。類似的問題是here

有兩種方法可以解決這個問題。

1.使用DataTemplate

<s:SurfaceListBox.ItemTemplate> 
    <DataTemplate> 
     <Expander Header="{Binding Description}" 
        IsExpanded="True" > 
      <ItemsControl ItemsSource={Binding substeps} /> 
     </Expander > 
    </DataTemplate> 
</s:SurfaceListBox.ItemTemplate> 

2.使用TemplatedParent結合

<Expander Header="{Binding Content.Description, , RelativeSource={RelativeSource TemplatedParent}}" 
      IsExpanded="True"> 

我個人推薦選擇1

+0

啊,做的伎倆。謝謝。但最後一個問題:擴展頭的{綁定描述}似乎不起作用。它總是空的(Header =「test」顯示「test」)描述是我的Task-class的一個屬性。我是否需要在某處設置數據源?這很奇怪,因爲{Binding substeps}工作得很好。 – StefanG

+0

請參閱編輯擴展標題答案。 –