【问题标题】:XAML: Custom Binding in DataTemplate for use in GridViewColumn CellTemplateXAML:DataTemplate 中的自定义绑定以在 GridViewColumn CellTemplate 中使用
【发布时间】:2014-05-31 01:06:38
【问题描述】:

我希望在整个 GridView 中重用的资源中有以下 DataTemplate。

<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 等)使用自定义绑定。

我设想这样做的方式是将 TextBlock 的绑定更改为简单地使用 {Binding} 并让 GridViewColumn 设置绑定。但是,我不确定在哪里或如何执行此操作,因为将 DisplayMemberValue 设置为 {Binding MyLength}(例如)只会覆盖模板。

我希望完全在 XAML 中完成此操作。

【问题讨论】:

    标签: wpf xaml binding datatemplate


    【解决方案1】:

    当我们设置了DisplayMemberBinding 属性时,似乎CellTemplate 总是会被忽略。此限制的可能解决方法是,通过创建@H.B 在他对类似问题here 的回答中指出的标记扩展。创建标记扩展涉及C#/VB代码,但使用它只需要XAML代码。

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

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

    现在您可以对不同的列 binidng 使用相同的 DataTemplate

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

    【讨论】:

    • 在这种情况下 TemplateBuilder 类是什么。你能告诉我们相同的大纲吗
    猜你喜欢
    • 2012-06-28
    • 1970-01-01
    • 2021-12-30
    • 2013-01-17
    • 2011-09-15
    • 2016-09-23
    • 2012-04-01
    • 1970-01-01
    • 2012-10-19
    相关资源
    最近更新 更多