如何将DataTemplate添加到CollectionContainer?

时间:2016-10-11 03:38:33

标签: c# wpf datatemplate

这是我的具体情况。

Window资源代码:

...
<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>
...

ViewModel(假设它正确绑定)

...
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>();
...

Class CutomObject(可能不需要显示):

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

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

假设ObservableCollection s具有适当的内容。

我的问题是:如何正确显示收藏品? 以下是标准:

  • 在第一行,会有一个TextBlock,里面有一个Text,上面写着“Sample Text”
  • 接下来是一个带有“Command”标签的按钮
  • 下一行(多达ViewModelObservableCollection_String个项目)是TextBlocks。其文本应为ViewModelObservableCollection_String
  • 的单个项目的值
  • 下一行(多达ViewModelObservableCollection_CustomObject项)是TextBoxes。其文本应为ViewModelObservableCollection_CustomObject的单个项目的值(firstStringsecondString的串联)。

正如您所看到的,StackPanel的内容是多个Collection与不同DataTemplate的合并。

如果事情不够清楚,请询问澄清。

1 个答案:

答案 0 :(得分:1)

  1. 使用from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By from time import sleep import unittest class Test_PythonOrg(unittest.TestCase): def setUp(self): self.browser = webdriver.Firefox() def tearDown(self): sleep(4) self.browser.close() self.browser = None def test_07_ListOfTodayDeals(self): self.browser.get("http://www.amazon.com") deals = self.browser.find_element_by_link_text("Today's Deals") deals.click() sleep(5) self.browser.find_element_by_xpath("//span[@class='a-label a-checkbox-label' and contains(text(),'Books')]/preceding-sibling::input[1]").click() self.browser.close() if __name__ == "__main__": unittest.main(verbosity=2) 内的DataTrigger更改ItemTemplate使用的ControlTemplate,同时比较Control。为此使用一个将返回类型的转换器。

    或,

  2. Type用作ContentControl

  3. 定义ItemTemplate,其中包含DataTemplateDataType会自动为其ContentControl选择适当的DataTemplate

  4. 第二种方法(推荐)

    ContentTemplate
相关问题