如何为GridView定义一个ItemTemplate(在ListView内部),以便可以在多个不同的ListView中使用? [英] How to define an ItemTemplate for GridView (inside a ListView) so it can be used in multiple different ListViews?

查看:89
本文介绍了如何为GridView定义一个ItemTemplate(在ListView内部),以便可以在多个不同的ListView中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 ListView ,它们绑定到ViewModel中的集合.这些集合中项目的类型相同(我们称它们为 TypeA ),这是一个暴露多个简单类型属性的类.我想在 ListView 内的 GridView 中显示它们.自然,我想为此 TypeA 定义一个 DataTemplate ,这样我就不必为每个 ListView 复制相同的XAML.的.到目前为止,我发现的所有示例都在 ListView 中定义了 ItemTemplate .如何创建资源并让不同的 ListView 引用此单个资源?

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?

更新:

为了更好的说明,我正在发布我的XAML代码.我原始的XAML代码(简体)如下所示:

I am posting my XAML code for better clarification. My original XAML code (simplified) looks like this:

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

所以我的想法是我可以在不同的 GridView 中以相同的方式显示相同类型的不同选择(因为我们希望列标题显示属性的名称,因此我们使用 GridView ).

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

顺便说一句,这些并不是我要介绍的唯一数据类型,因此,我绝对需要指定一个Resource键并将其限制为仅用于GridView.并且因为我想以网格方式显示,所以我假设无法为我的类型定义 DataTemplate 并将其用作 ListView 中的 ItemTemplate >.

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.

推荐答案

您甚至不必定义资源密钥,只需设置DataType,因为WPF将在内部将其用作密钥.只要确保您的数据模板在您要使用它的任何控件中可见,它们就会被自动应用.(例如,您可以在App.xaml的资源中在应用程序级别定义它们,但是您可能具有单独的资源字典):

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):

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

对于您的代码,您需要为每个属性定义模板,因为您将其用作网格,并且需要在删除DisplaymemberBinding之后为每个列设置单元格模板:

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:

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

您可以像这样使用ColumnHeaderTemplate和ColumnContainerStyle:

You can use a ColumnHeaderTemplate and a ColumnContainerStyle like this:

     <GridView
           ColumnHeaderTemplate="{StaticResource myHeaderTemplate}"
           ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}"
     >
           <GridViewColumn Header="Prop1" CellTemplate="{StaticResource prop1Template}"/>
     ...

例如,其中的资源:

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

这篇关于如何为GridView定义一个ItemTemplate(在ListView内部),以便可以在多个不同的ListView中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆