2014-10-01 25 views
1

所以一個DataTemplate,這個想法是,我有一個ListView使用Template1作爲DataTemplate,然後包含Template2另一個ListView,使用該結合/的datacontext第一Cars。所以DataContextTemplate2應該是Template1DataContext.Cars它應該被用作DataTemplate2的綁定。創建包含另一個的DataTemplate其中使用的數據源從第一

可能嗎?如何???

型號:

class Reseller 
{ 
    public Dictionary<int, Car> Cars; 
    ... 
} 

class Car 
{ 
    public string Model { get; set; } 
    public string Year { get; set; } 
    ... 
} 

我有兩個的DataTemplates這樣的:

<!--Template1 contains a Dictionary<string, Reseller>--> 
<!-- Reseller has another dictionary of Cars like Dictionary<int, Car>--> 
<DataTemplate x:Key="Template1"> 
    <StackPanel Orientation="Horizontal"> 
     <GroupBox Header="{Binding Key}"> 
      <ListView x:Name="Internal_Template2 
         ItemTemplate="{StaticResource Template2}"/> 
     </GroupBox> 
    </StackPanel> 
</DataTemplate> 

<DataTemplate x:Key="Template2"> 
    <Grid> 
     <TextBlock Text="{Binding Template1.Value.Cars.Model}"/> 
     <TextBlock Text="{Binding Template1.Value.Cars.Year}"/> 
    </Grid> 
</DataTemplate> 

這裏是什麼,我想創建一個快速圖紙。 BranchX是ListView,包含Cars這是另一個'的ListView,從汽車BranchX的屬性: enter image description here

回答

1

當分配的ItemsSource(我假設你在代碼隱藏)在的ListView,每個項目的的DataContext設置爲自己的模型表示。例如:

ListView (DataContext: Resellers) 
|-- ListViewItem (DataContext: Resellers[0]) 
|-- ListViewItem (DataContext: Resellers[1]) 
`-- etc... 

所以你不需要訪問父的DataTemplate的DataContext。實際上,DataContext模板2自動設置爲Resellers[].Value.Cars[]。這允許您將的TextBlock小號綁定到Value

<DataTemplate x:Key="Template2"> 
    <Grid> 
     <TextBlock Text="{Binding Value.Model}"/> 
     <TextBlock Text="{Binding Value.Year}"/> 
    </Grid> 
</DataTemplate> 

<DataTemplate x:Key="Template1"> 
    <GroupBox Header="{Binding Key}"> 
     <ListView x:Name="Internal_Template2 
        ItemsSource="{Binding Value.Cars}" 
        ItemTemplate="{StaticResource Template2}"/> 
    </GroupBox> 
</DataTemplate> 

你不能讓的ListView顯示其項目在ItemTemplate中水平,需要將ItemsPanel屬性添加到您的經銷商ListView

<ListView.ItemsPanel> 
    <ItemsPanelTemplate> 
     <StackPanel 
      Orientation="Horizontal" /> 
    </ItemsPanelTemplate> 
</ListView.ItemsPanel> 
+0

感謝您的貢獻。這很簡單,我使用的解決方案。它也可以通過數據源的精彩,完全忘了! :)我只需要記住,屬性需要是公開的,字符串類型或int。 – grmihel 2014-10-02 08:51:27

2

是的,這是可能的。我將向您展示適用於我的項目的示例,其中我在另一個ItemsControl中使用ItemsControl。這可以適應其他容器比ItemsControl,但ItemsControl提供了最大的靈活性。 如果我理解正確,您希望顯示經銷商列表,並且要爲每個經銷商顯示汽車列表。

對於每個ResellerInfo項目,外部ItemsControl將顯示帶有TextBlock的StackPanel和顯示Cars的另一個ItemsControl。

內部ItemsControl然後顯示一個StackPanel與每輛車的模型和年份。請注意,內部的DataContext自動設置爲CarInfo的實例。

作爲獎勵,我們使用VirtualizingStackPanel,它不會渲染離開屏幕的項目 - 如果數據庫很大,這可以派上用場。

視圖模型這是主控制的的DataContext需要定義:

public ObservableCollection<ResellerInfo> Resellers 

ResellerInfo是具有屬性的類:

public String ResellerName 
public ObservableCollection<CarInfo> Cars 

CarInfo是一類有屬性:

所有屬性

二傳手需要調用

NotifyPropertyChanged(); 
這是在 INotifyPropertyChanged接口中定義

XAML:

 <ItemsControl ItemsSource="{Binding Resellers}" > 
      <!-- Template specifies how this ItemsControl looks like --> 
      <ItemsControl.Template> 
       <ControlTemplate TargetType="ItemsControl"> 
        <ScrollViewer Margin="5"> 
         <ItemsPresenter /> 
        </ScrollViewer> 
       </ControlTemplate> 
      </ItemsControl.Template> 
      <!-- ItemsPanel holds items. Use it to change the way items are laid out --> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" Orientation="Horizontal" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <!-- ItemTemplate specifies how each item is displayed --> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <!-- BEGINNING OF SINGLE ITEM CODE --> 
        <StackPanel> 
         <TextBlock Text="{Binding ResellerName}" /> 
         <ItemsControl ItemsSource="{Binding Cars}"> 
          <!-- Template specifies how this ItemsControl looks like --> 
          <ItemsControl.Template> 
           <ControlTemplate TargetType="ItemsControl"> 
            <ScrollViewer Margin="5"> 
             <ItemsPresenter /> 
            </ScrollViewer > 
           </ControlTemplate> 
          </ItemsControl.Template> 
          <!-- ItemsPanel holds items. Use it to change the way items are laid out --> 
          <ItemsControl.ItemsPanel> 
           <ItemsPanelTemplate> 
            <VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" Orientation="Horizontal" /> 
           </ItemsPanelTemplate> 
          </ItemsControl.ItemsPanel> 
          <!-- ItemTemplate specifies how each item is displayed. --> 
          <ItemsControl.ItemTemplate> 
           <DataTemplate> 
            <StackPanel> 
             <TextBlock Text="{Binding Model}" /> 
             <TextBlock Text="{Binding Year}" /> 
            </StackPanel> 
           <DataTemplate> 
          </ItemsControl.ItemTemplate> --> 
         </ItemsControl> 
        </StackPanel> 
        <!-- END OF SINGLE ITEM CODE --> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

我這裏寫的代碼在計算器上,所以請隨時糾正如果你發現錯誤!

+0

感謝您的貢獻。毫無疑問,這個解決方案很適合VirtualizingPanel。我使用@hantoun的示例,因爲UI僅用於調試,並且該示例很簡單。但你的解決方案也解決了我的問題:) – grmihel 2014-10-02 08:54:16

+0

儘管這個解決方案比我發佈的解決方案更好。我剛剛詳細說明了你已有的代碼。 – Sjeijoet 2014-10-02 09:02:45

+0

@hantoun你的解決方案明確回答這個問題。其實,我不知道這可以用這麼簡單的代碼來完成! – 2014-10-02 15:31:01

相關問題