2008-12-09 80 views
1

我有一些數據有一個詳細表。我希望數據在ListView中呈現。當你選擇原始列表中的一個項目時,我希望細節數據顯示爲嵌套的ListView。我似乎無法弄清楚如何讓數據綁定工作。在WPF中嵌套ListViews的分層數據綁定

這裏是我到目前爲止,(問題是{Binding Path=FK_History_HistoryItems}):

<ListView Name="lstHistory" ItemsSource="{Binding Source={StaticResource History}}" SelectionChanged="lstHistory_SelectionChanged"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" Width="100" /> 
      <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" Header="Description" Width="150" /> 
      <GridViewColumn DisplayMemberBinding="{Binding Path=Total, Converter={StaticResource moneyConvert}}" Header="Total" Width="100" /> 
      <GridViewColumn DisplayMemberBinding="{Binding Converter={StaticResource categoryAggregate}}" Header="Categories" Width="100" /> 
     </GridView> 
    </ListView.View> 
    <ListView.Resources> 
     <Style TargetType="{x:Type ListViewItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListViewItem}"> 
         <Border> 
          <StackPanel> 
           <Border Name="presenter" 
             Background="{TemplateBinding Background}" 
             BorderBrush="{TemplateBinding BorderBrush}" 
             BorderThickness="{TemplateBinding BorderThickness}" 
             Padding="{TemplateBinding Padding}"> 
            <GridViewRowPresenter /> 
           </Border> 
           <Border Name="details" Visibility="Collapsed" Margin="5" 
             BorderBrush="Black" BorderThickness="2"> 
            <StackPanel Margin="5"> 
             <ListView ItemsSource="{Binding Path=FK_History_HistoryItems}"> 
              <ListView.View> 
               <GridView> 
                <GridViewColumn DisplayMemberBinding="{Binding Path=Ammount}" Header="Ammount" Width="100" /> 
                <GridViewColumn DisplayMemberBinding="{Binding Path=Category}" Header="Category" Width="100" /> 
               </GridView> 
              </ListView.View> 
             </ListView> 
            </StackPanel> 
           </Border> 
          </StackPanel> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="True"> 
           <Setter TargetName="details" Property="Visibility" Value="Visible" /> 
           <Setter TargetName="presenter" Property="Background" Value="Navy"/> 
           <Setter TargetName="presenter" Property="TextElement.Foreground" Value="White" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListView.Resources> 
</ListView> 
+0

你從綁定中得到什麼輸出不起作用? (只是所以你知道,「金額」有一個「米」......) – Donnelle 2008-12-09 22:25:24

+0

它適用於我的相同的數據設置http://stackoverflow.com/questions/350214/wpf-use-a-列表框-IN-A-模板 - 即,將待的模板 - 的 - 另一個-列表框。您使用的數據設置和來源是什麼? – Donnelle 2008-12-09 23:44:43

回答

1

如果我正確理解你的問題,你需要綁定到原始列表的的SelectedItem:

<ListView ItemsSource="{Binding ElementName=lstHistory, Path=SelectedItem}"> 

然後根據需要設置數據模式/視圖。如果你不想爲綁定使用ElementName,你也可以使用RelativeSource,但是我發現ElementName更易於閱讀和理解。

0

您需要將您的問題行更改爲以下:

<ListView ItemsSource="{Binding FK_History_HistoryItems}"> 

隨着這種變化,控制精美的作品。我一直在做一些類似無用的事情。我真的很喜歡你的工作。

0

爲了得到觸發工作,你將需要設置的ControlTemplate的TargetType:

<ControlTemplate TargetType="{x:Type ListViewItem}"> 

沒有被的TargetType指定(作爲可選類型),將XAML渲染會迷茫......