wpf 如何为 GridView(在 ListView 内)定义一个 ItemTemplate 以便它可以在多个不同的 ListView 中使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18891036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define an ItemTemplate for GridView (inside a ListView) so it can be used in multiple different ListViews?
提问by tete
I have some ListView
's which bind to collections in the ViewModel. The type of items in those collections are the same (let's call them TypeA
) which is a class exposing multiple simple type properties. I would like to display them in a GridView
inside the ListView
. Naturally, I would want to define a DataTemplate
for this TypeA
so that I don't have to duplicate the same XAML for each of the ListView
's. All of the examples I've found so far define the ItemTemplate
inside the ListView
. How can I make a resource and let different ListView
's refer to this single resource?
我有一些ListView
绑定到 ViewModel 中的集合。这些集合中的项目类型是相同的(我们称之为TypeA
),这是一个公开多个简单类型属性的类。我想在显示他们GridView
里面ListView
。自然地,我想为此定义一个DataTemplate
,TypeA
这样我就不必为每个ListView
'复制相同的 XAML 。到目前为止,我发现的所有示例都定义ItemTemplate
了ListView
. 我怎样才能制作一个资源并让不同的ListView
's 引用这个单一的资源?
UPDATE:
更新:
I am posting my XAML code for better clarification. My original XAML code (simplified) looks like this:
我发布了我的 XAML 代码以便更好地说明。我的原始 XAML 代码(简化)如下所示:
<ListView ItemsSource="{Binding MYCOLLECTION}" SelectionMode="Extended" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Prop1}" >
<GridViewColumn.Header>
<Border >
<TextBlock Text="Prop1" />
</Border>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Prop2}" >
<GridViewColumn.Header>
<Border >
<TextBlock Text="Prop2" />
</Border>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Prop3}" >
<GridViewColumn.Header>
<Border >
<TextBlock Text="Prop3" />
</Border>
</GridViewColumn.Header>
</GridViewColumn>
...
</GridView>
</ListView.View>
</ListView>
I have many dependency properties set to the headers and that's why I put individual Header items as well as why my code is really long--hence why I'm seeking to use a datatemplate.
我为标题设置了许多依赖项属性,这就是为什么我要放置单个标题项以及为什么我的代码很长的原因——因此为什么我要使用数据模板。
So my idea is that I can show different selections of the same type in the same way in different GridView
's (because we want the column headers to display the names of the properties so we use GridView
).
所以我的想法是我可以在不同的GridView
's中以相同的方式显示相同类型的不同选择(因为我们希望列标题显示属性的名称,所以我们使用GridView
)。
BTW those are not the only places I am presenting this data type, so I definitely need to specify a Resource key and restrict this to be used only for GridView. And because I want to display in a Grid way, I assume can't define a DataTemplate
for my type and use it as ItemTemplate
in a ListView
.
顺便说一句,这些不是我呈现这种数据类型的唯一地方,所以我绝对需要指定一个资源键并将其限制为仅用于 GridView。因为我想在一个网格的方式显示,我认为不能定义DataTemplate
为我喜欢的类型,并把它作为ItemTemplate
一个ListView
。
采纳答案by Chevul Ervin
You do not even have to define a resource key just set the DataType because that will be used as a key internally by WPF. Just make sure your datatemplates are visible from any control where you want to use it, they will be automatically applied. (Just for example you can define them at application level in the resources of App.xaml, but you probably have separate resource dictionaries):
您甚至不必定义资源键,只需设置 DataType,因为 WPF 将在内部将其用作键。只要确保您的数据模板在您想要使用它的任何控件中都是可见的,它们将被自动应用。(例如,您可以在 App.xaml 的资源中在应用程序级别定义它们,但您可能有单独的资源字典):
<Application x:Class="WpfTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dataModel="clr-namespace:DataModel"
StartupUri="MainWindow.xaml">
<Application.Resources>
<DataTemplate DataType="{x:Type dataModel:GreekGod}">
<TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
</DataTemplate>
</Application.Resources>
</Application>
For your code you need to define templates for each property because you are using it as a grid and you need to set the cell template for each column after removing the DisplaymemberBinding:
对于您的代码,您需要为每个属性定义模板,因为您将其用作网格,并且需要在删除 DisplaymemberBinding 后为每列设置单元格模板:
<GridViewColumn CellTemplate="{StaticResource prop1Template}">
<GridViewColumn.Header>
<Border >
<TextBlock Text="Prop1" />
</Border>
</GridViewColumn.Header>
</GridViewColumn>
...
And put the resource in a visible place just as mentioned before like in the Application resources:
并将资源放在可见的位置,就像之前提到的应用程序资源一样:
<Application.Resources>
<DataTemplate x:Key="prop1Template">
<TextBlock Text="{Binding Prop1}" Foreground="Red"/>
</DataTemplate>
</Application.Resources>
You can use a ColumnHeaderTemplate and a ColumnContainerStyle like this:
您可以像这样使用 ColumnHeaderTemplate 和 ColumnContainerStyle:
<GridView
ColumnHeaderTemplate="{StaticResource myHeaderTemplate}"
ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}"
>
<GridViewColumn Header="Prop1" CellTemplate="{StaticResource prop1Template}"/>
...
where the resources are for example:
例如,资源在哪里:
<Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background" Value="LightBlue"/>
</Style>
<DataTemplate x:Key="myHeaderTemplate">
<DockPanel>
<CheckBox/>
<TextBlock FontSize="16" Foreground="DarkBlue">
<TextBlock.Text>
<Binding/>
</TextBlock.Text>
</TextBlock>
</DockPanel>
</DataTemplate>
回答by Florian Gl
Just create the DataTemplate
in the Controls/Windows resources as you would do in the ListViews ItemTemplate and add x:Key="someKey"
to it.
只需DataTemplate
像在 ListViews ItemTemplate 中一样在 Controls/Windows 资源中创建并添加x:Key="someKey"
到其中即可。
Now you can refer to it like so:
现在你可以这样引用它:
<ListView ItemTemplate="{StaticResource someKey}"/>