【问题标题】:How to bind through to a template如何绑定到模板
【发布时间】:2020-12-07 21:29:06
【问题描述】:

我正在尝试将TextBox(和ComboBox,当我得到这个工作时)添加到默认的DataGridColumnHeader,并且已经像下面那样做了。这是在一个应用程序中ResourceDirectory^:

<Style x:Key="DataGridColumnHeaderStyle_TextBox" TargetType="{x:Type DataGridColumnHeader}">
   <Setter Property="VerticalContentAlignment" Value="Center"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
            <Themes:DataGridHeaderBorder Grid.Row="0" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
               <Grid>
                  <Grid.RowDefinitions>
                     <RowDefinition Height="*"/>
                     <RowDefinition Height="*"/>
                  </Grid.RowDefinitions>
                  <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                  <TextBox Grid.Row="1" Text="" HorizontalAlignment="Stretch" BorderThickness="1" />
                  <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                  <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
               </Grid>
            </Themes:DataGridHeaderBorder>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

在我的 Window DataGrid 我有以下内容:

<DataGridTextColumn Header="PO Number" Binding="{Binding PO_Number}" HeaderStyle="{DynamicResource DataGridColumnHeaderStyle_TextBox}"/>

这看起来是我现在想要的方式,Header 文本绑定到 ContentPresenter

问题

如何设置它,以便可以像这样绑定到新添加的 TextBox(或任何对象):HeaderTemplate.TextBox.Text="{Binding SomeSubTotalClass}"

<DataGridTextColumn Header="PO Number" HeaderTemplate.TextBox.Text="{Binding SomeSubTotalClass}" Binding="{Binding PO_Number}" HeaderStyle="{DynamicResource DataGridColumnHeaderStyle_TextBox}"/>

【问题讨论】:

    标签: c# wpf xaml datagrid controltemplate


    【解决方案1】:

    您可以为自定义列标题创建一组附加属性。

    public static class DataGridHeaderProperties
    {
       public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
          "Text", typeof(string), typeof(DataGridHeaderProperties));
    
       public static string GetText(DependencyObject dependencyObject)
       {
          return (string)dependencyObject.GetValue(TextProperty);
       }
    
       public static void SetText(DependencyObject dependencyObject, string value)
       {
          dependencyObject.SetValue(TextProperty, value);
       }
    }
    

    然后调整您的列标题模板,以便TextBoxText 属性绑定到在相应数据网格列定义中指定的附加属性。

    <TextBox Grid.Row="1" Text="{Binding Column.(local:DataGridHeaderProperties.Text), RelativeSource={RelativeSource AncestorType={x:Type DataGridColumnHeader}}}" HorizontalAlignment="Stretch" BorderThickness="1" />
    

    现在,在列定义上设置属性。下面,只有一个静态文本用于测试,但你可以任意绑定属性。

    <DataGridTextColumn Header="PO Number"
                        Binding="{Binding PO_Number}"
                        HeaderStyle="{StaticResource DataGridColumnHeaderStyle_TextBox}"
                        local:DataGridHeaderProperties.Text="My custom text"/>
    

    但是,请注意,列不是可视化树的一部分,也没有要绑定的数据上下文,因此您必须使用binding proxy 或其他机制。这是DataGrid 的一个众所周知的问题,不是这个问题的核心,但 StackOverflow 上已经有解决方案。

    您可以将此解决方案扩展到列标题中的任何其他控件,例如ComboBox。您只需要添加其他附加属性,例如对于ItemsSourceSelectedItem 等等,但你也应该投入一些来完善这个概念。将这些类型的控件放在列标题中似乎很奇怪。它可能会损害您的应用程序的可用性。也许有更适合您要求的方法。

    【讨论】:

    • 谢谢,我会努力实现这一点。我真的只是想在我对 xmal 的学习中更进一步。从概念上讲,组合框类似于 Excel 的数据过滤器,而文本框是列的小计。我会报告的
    • 这部分效果很好。现在正在处理绑定代理。不同的问题,所以我可以接受这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多