XAML:在GridViewColumn CellTemplate中使用的DataTemplate中的自定义绑定 [英] XAML: Custom Binding in DataTemplate for use in GridViewColumn CellTemplate

查看:352
本文介绍了XAML:在GridViewColumn CellTemplate中使用的DataTemplate中的自定义绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在整个GridView中重用资源中的以下DataTemplate.

I have the following DataTemplate in resources that I would like to reuse throughout a GridView.

<Window.Resources>
  <DataTemplate x:Key="NumericalDataTemplate" DataType="GridViewColumn.CellTemplate">
     <StackPanel Orientation="Horizontal" Height="32">
       <TextBlock Text="{Binding MyLength}" VerticalAlignment="Center"
                     HorizontalAlignment="Center">
         <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}" >
               <Setter Property="Visibility" Value="Visible" />
               <Style.Triggers>
                 <DataTrigger Binding="{Binding PropertyEditable}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed" />
                 </DataTrigger>
               </Style.Triggers>
           </Style>
         </TextBlock.Style>
       </TextBlock>
    </StackPanel>
  </DataTemplate>
</Window.Resources>

实现如下.

<GridViewColumn Header="MyLength" Width="80" 
                CellTemplate="{StaticResource NumericalDataTemplate}" />

我想更改TextBlock的绑定(当前为Text={Binding MyLength}),以便它可以为每个GridViewColumn单元格模板(例如MyHeight,MyWeight等)使用自定义绑定.

I would like change the Binding of the TextBlock (Currently Text={Binding MyLength} ) so that it can use a custom binding for each GridViewColumn Cell Template (eg MyHeight, MyWeight etc).

我设想的方法是将TextBlock的Binding更改为仅使用{Binding}并让GridViewColumn设置Binding.但是,我不确定在何处或如何执行此操作,因为例如将DisplayMemberValue设置为{Binding MyLength}只会覆盖模板.

The way I envisaged doing this is changing the Binding of the TextBlock to simply use {Binding} and having the GridViewColumn set the Binding. However, I'm not sure where or how to do this, as setting the DisplayMemberValue to {Binding MyLength} (for example) simply overrides the template.

我最好完全在XAML中做到这一点.

I would preferably like to do this entirely in XAML.

推荐答案

当设置了DisplayMemberBinding属性时,似乎总是会忽略CellTemplate.针对此限制的可能解决方法是,通过创建 @HB 在他对类似问题此处.创建标记扩展名涉及C#/VB代码,但使用它只需要XAML代码.

It seems that CellTemplate will always be ignored when we have DisplayMemberBinding property set. Possible workaround for this limitation is, by creating markup-extension as pointed by @H.B in his answer to similar question here. Creating markup-extension involves C#/VB codes, but using it only needs XAML codes.

您可以重用@ H.B提供的相同的标记扩展C#代码.然后在XAML中使用它,声明名称空间前缀:

You can reuse the same markup-extension C# code provided by @H.B. Then to use it in your XAML, declare namespace prefix :

<Window ......
    xmlns:local="clr-namespace:WpfProject">

修改DataTemplate键和内部的TextBlock绑定:

<DataTemplate x:Key="TemplateBuilder_BaseTemplate" DataType="GridViewColumn.CellTemplate">
    <StackPanel Orientation="Horizontal" Height="32">
        <TextBlock Text="{local:TemplateBuilderTag}" VerticalAlignment="Center"
                   HorizontalAlignment="Center">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}" >
                    <Setter Property="Visibility" Value="Visible" />
                    <Setter Property="Foreground" Value="Red"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding PropertyEditable}" Value="True">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </StackPanel>
</DataTemplate>

现在,您可以将相同的DataTemplate用于不同的列binidngs:

Now you can use the same DataTemplate for different column binidngs :

<GridView.Columns>
    <GridViewColumn Header="MyLength" Width="80"
            CellTemplate="{local:TemplateBuilder MyLength}" />
    <GridViewColumn Header="MyHeight" Width="80" 
            CellTemplate="{local:TemplateBuilder MyHeight}" />
</GridView.Columns>

这篇关于XAML:在GridViewColumn CellTemplate中使用的DataTemplate中的自定义绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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