2016-10-11 62 views
-1

這是我的具體情況。如何將一個DataTemplate添加到CollectionContainer?

的窗口資源的代碼:

... 
<Window.Resources> 
    <ResourceDictionary> 
     <CollectionViewSource x:Key="AdditionalStringData" Source="{Binding ViewModelObservableCollection_String}"/> 
     <CollectionViewSource x:Key="AdditionalCustomObjectData" Source="{Binding ViewModelObservableCollection_CustomObject}"/> 
     <ResourceDictionary.MergedDictionaries> 
      ... 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
... 

其中我需要顯示集合中的部分:

... 
<StackPanel> 
    <ItemsControl> 
     <ItemsControl.ItemsSource> 
      <CompositeCollection> 
       <TextBlock Text="{Binding ViewModelTextProperty}"/> 
       <Button Command="{Binding ViewModelRelayCommand}">Command</Button> 
       <CollectionContainer Collection="{Binding Source={StaticResource AdditionalStringData}}" /> 
       <CollectionContainer Collection="{Binding Source={StaticResource AdditionalCustomObjectData}}" /> 
      </CompositeCollection> 
     </ItemsControl.ItemsSource> 
    </ItemsControl> 
</StackPanel> 
... 

的視圖模型(假定其被正確地綁定)

... 
private string ViewModelTextProperty { get; set; } = "Sample Text"; 
public RelayCommand ViewModelRelayCommand { ... } 
private ObservableCollection<string> ViewModelObservableCollection_String { get; set; } = new ObservableCollection<string>(); 
private ObservableCollection<CustomObject> ViewModelObservableCollection_CustomObject { get; set; } = new ObservableCollection<CustomObject>(); 
... 

類CutomObject(可能不需要顯示):

... 
public class CustomObject 
{ 
    public string firstString; 
    public string secondString; 

    public CustomObject() 
    { 
     ... 
    } 
    ... 
} 
... 

假設ObservableCollection具有適當的內容。

我的問題是:如何正確顯示集合? 這裏是標準:

  • 在第一行,就會有一個內含文本的TextBlock,上面寫着「樣品文本」
  • 接下來是一個標籤「命令」
  • 下一個按鈕行(多達ViewModelObservableCollection_String項)是TextBlocks。其文本應該是ViewModelObservableCollection_String單個項目的值。
  • 下一行(多達ViewModelObservableCollection_CustomObject項)是TextBoxes。其文本應爲ViewModelObservableCollection_CustomObject(連接firstStringsecondString)的單個項目的值。

正如你所看到的,StackPanel的內容是不止一個Collection的合併,具有不同的DataTemplate

如果事情不夠清楚,請詢問澄清。

回答

1
  1. 使用DataTriggerItemTemplate改變使用ControlControlTemplate,而比較Type。爲此使用將返回類型的轉換器。

    ,或者

  2. 使用ContentControlItemTemplate

  3. 定義DataTemplate指定DataType在其中。 ContentControl將自動爲ContentTemplate選擇合適的DataTemplate

方法二(推薦)

<Window.Resources> 
    <ResourceDictionary>    
     ... 
     <DataTemplate DataType="{x:Type sys:String}"> 
      <TextBlock Background="ForestGreen" Text="{Binding .}"/> 
     </DataTemplate> 

     <DataTemplate DataType="{x:Type local:CustomObject}"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Background="Red" Text="{Binding firstString}"/> 
       <TextBlock Background="Red" Text="{Binding secondString}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ResourceDictionary> 
</Window.Resources> 
<ItemsControl> 
... 
<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <ContentControl Content="{Binding .}"/> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
... 
</ItemsControl> 
+0

感謝。你能否詳細說明一下?我對WPF比較陌生。 – someone

+0

我認爲,你的第二個建議更容易。你能提供一個示例代碼嗎?如果沒有,請給我一些參考。 – someone

相關問題