在多个GridViewColumns使用的通用的DataTemplate [英] Generic DataTemplate used in multiple GridViewColumns

查看:130
本文介绍了在多个GridViewColumns使用的通用的DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示一些值的GridView:

 < ListView控件的ItemsSource ={结合MyDataSource}>
    < ListView.View>
        <&GridView的GT;
            < GridViewColumn标题=日期1DisplayMemberBinding ={结合日期1}/>
            < GridViewColumn标题=日期2DisplayMemberBinding ={结合日期2}/>
            ...等栏目,不一定包含日期...
        < / GridView的>
    < /ListView.View>
< /&的ListView GT;

这工作正常。现在,我想创建一个格式化日期以特定的方式数据模板:

 <的DataTemplate X:键=MySpecialDate>
    < TextBlock的文本={绑定的StringFormat = {} {0:YYYY.MM.DD}}/>
< / DataTemplate中>

添加指定 CellTemplate 将无法工作,只要 DisplayMemberBinding 。因此,我不得不删除 DisplayMemberBinding 属性:

 < GridViewColumn标题=日期1CellTemplate ={StaticResource的MySpecialDate}/>
< GridViewColumn标题=日期2CellTemplate ={StaticResource的MySpecialDate}/>

这里的问题:现在 DisplayMemberBinding 走了,我怎么告诉GridView控件中显示的财产? GridViewColumn 不具有的DataContext 属性。

当然,我可以把属性的名称(日期1 RESP。日期2 )插入到DataTemplate中,但我需要一个模板每个属性,而且会破坏其整个目的的模板

 <! - 我不希望出现这种情况 - >
<的DataTemplate X:键=MySpecialDate1>
    < TextBlock的文本={结合日期1,的StringFormat = {} {0:YYYY.MM.DD}}/>
< / DataTemplate中>
<的DataTemplate X:键=MySpecialDate2>
    < TextBlock的文本={结合日期2,的StringFormat = {} {0:YYYY.MM.DD}}/>
< / DataTemplate中>


解决方案

有关你的问题都是我的传递多个资源投入到一个DataTemplate ,我终于找到了一个解决方案。
我觉得你可以不在身边每一个这样的日期模板的定义得到。但是,这取决于你的显示模板的复杂性,这种解决方案保持了额外的的DataTemplate 取值code到最低限度:

 <的DataTemplate X:键=德恩>
  <内容presenter的ContentTemplate ={StaticResource的MySpecialDate}地方:YourClass.AttachedDate =德恩/>
< / DataTemplate中>

这些额外的的DataTemplate 的使用附加属性然后可以通过在的DataTemplate MySpecialDate转换器中使用。
附加的属性在code被定义后面。这个问题的答案我自己的问题包含完整的例子

I have a GridView that displays some values:

<ListView ItemsSource="{Binding MyDataSource}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Date1" DisplayMemberBinding="{Binding Date1}" />
            <GridViewColumn Header="Date2" DisplayMemberBinding="{Binding Date2}" />
            ...other Columns, not necessarily containing dates...
        </GridView>
    </ListView.View>
</ListView>

This works fine. Now I want to create a data template that formats a date in a specific way:

<DataTemplate x:Key="MySpecialDate">
    <TextBlock Text="{Binding StringFormat={}{0:yyyy.MM.dd}}" />
</DataTemplate>

Adding CellTemplate won't work as long as DisplayMemberBinding is specified. Thus, I have to remove the DisplayMemberBinding attribute:

<GridViewColumn Header="Date1" CellTemplate="{StaticResource MySpecialDate}" />
<GridViewColumn Header="Date2" CellTemplate="{StaticResource MySpecialDate}" />

Here's the question: Now that DisplayMemberBinding is gone, how do I tell the GridView which property to display? GridViewColumn does not have a DataContext property.

Of course, I could put the name of the property (Date1 resp. Date2) into the DataTemplate, but then I would need one template for each property and that would defeat the whole purpose of having a template.

<!-- I don't want that -->
<DataTemplate x:Key="MySpecialDate1">
    <TextBlock Text="{Binding Date1, StringFormat={}{0:yyyy.MM.dd}}" />
</DataTemplate>
<DataTemplate x:Key="MySpecialDate2">
    <TextBlock Text="{Binding Date2, StringFormat={}{0:yyyy.MM.dd}}" />
</DataTemplate>

解决方案

Related to your question is mine: Pass multiple resources into a DataTemplate which I finally found a solution for. I think you cannot get around the definition of a template for every such date. But depending on the complexity of your display template this solution keeps the additional DataTemplates code to a minimum:

<DataTemplate x:Key="DateX">
  <ContentPresenter ContentTemplate="{StaticResource MySpecialDate}" local:YourClass.AttachedDate="DateX"/>
</DataTemplate>

These additional DataTemplates use an attached property which can then be used by a converter in the DataTemplate "MySpecialDate". The attached property has to be defined in the code behind. The answer to my own question contains a complete example.

这篇关于在多个GridViewColumns使用的通用的DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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