0

我希望我的数据网格列共享一个单元格/单元格编辑模板。

我有解决方案(感谢WPF DataGridTemplateColumn 共享模板?)。现在我想要的是通过避免所有节点嵌套来提高可读性。

我目前的看法是这样的:

  <wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">

    <wpftk:DataGrid.Resources>
      <DataTemplate x:Key="CustomCellTemplate">
        <TextBlock Text="{TemplateBinding Content}"/>
      </DataTemplate>
      <DataTemplate x:Key="CustomCellEditingTemplate">
        <TextBox Text="{TemplateBinding Content}"></TextBox>
      </DataTemplate>
    </wpftk:DataGrid.Resources>

    <wpftk:DataGrid.Columns>

      <wpftk:DataGridTemplateColumn Header="Start Date">
        <wpftk:DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <ContentPresenter ContentTemplate="{StaticResource CustomCellTemplate}" Content="{Binding StartDate}"/>
          </DataTemplate>
        </wpftk:DataGridTemplateColumn.CellTemplate>
        <wpftk:DataGridTemplateColumn.CellEditingTemplate>
          <DataTemplate>
            <ContentPresenter ContentTemplate="{StaticResource CustomCellEditingTemplate}" Content="{Binding StartDate}"/>
          </DataTemplate>
        </wpftk:DataGridTemplateColumn.CellEditingTemplate>
      </wpftk:DataGridTemplateColumn>

      <!--and again the whole block above for each columns...-->

    </wpftk:DataGrid.Columns>
  </wpftk:DataGrid>

我想要实现的是在DataGridTemplateColumn级别绑定值并将其传播到模板级别。有谁知道该怎么做?

我试图做的是这样的:

  <wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">

    <wpftk:DataGrid.Resources>
      <DataTemplate x:Key="CustomCellTemplate">
        <TextBlock Text="{Binding}"/>
      </DataTemplate>
      <DataTemplate x:Key="CustomCellEditingTemplate">
        <TextBox Text="{Binding}"></TextBox>
      </DataTemplate>
    </wpftk:DataGrid.Resources>

    <wpftk:DataGrid.Columns>
      <wpftk:DataGridTemplateColumn Header="Start Date" Binding="{Binding StartDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
      <wpftk:DataGridTemplateColumn Header="End Date" Binding="{Binding EndDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
    </wpftk:DataGrid.Columns>
  </wpftk:DataGrid>

显然,绑定属性不是有效的属性,DataGridTemplateColumn但也许通过使用数据上下文和一些相关源可以做到这一点,但坦率地说,我找不到实现它的方法。

不确定我想要的是否可能,我愿意接受“你不可能做到这一点”作为答案

注意:模板中的TextBlock/TextBox仅用于测试(真正的模板要复杂得多)DataGridTextColumn不会起作用提前谢谢

4

1 回答 1

1

XAML通过指定DataTemplates使用属性语法来减少 ,您尝试的内容应该有效。

编辑:对不起。我误解了你的问题。要在DataGridTemplateColumn不修改或访问源代码的情况下添加可绑定属性,您可以创建一个AttachedProperty.

例子:

public class DataBindingHelper 
{

    #region AttachedBinding

    public static readonly DependencyProperty AttachedBindingProperty = DependencyProperty.RegisterAttached("AttachedBinding", typeof(Binding), typeof(DataBindingHelper), new FrameworkPropertyMetadata(null));

    public static Binding GetUseAncestorDataContext(DependencyObject d)
    {
        return (bool)d.GetValue(AttachedBindingProperty);
    }

    public static void SetUseAncestorDataContext(DependencyObject d, Binding value)
    {
        d.SetValue(AttachedBindingProperty, value);
    }

    #endregion

}

用法:

<wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">

    <wpftk:DataGrid.Resources>
        <DataTemplate x:Key="NameTemplate">
            <TextBlock Text="{Binding}"/>
        </DataTemplate> 
        <DataTemplate x:Key="EditingTemplate">
            <TextBox Text="{Binding}"/>
        </DataTemplate>

        <DataTemplate x:Key="CustomCellTemplate">
            <ContentPresenter ContentTemplate="{StaticResource NameTemplate}" 
                              Content="{Binding Path=(helpers:DataBindingHelper.AttachedBinding), RelativeSource={RelativeSource AncestorType={x:Type wpftk:DataGridTemplateColumn}}}" />
        </DataTemplate>
        <DataTemplate x:Key="CustomCellEditingTemplate">
            <ContentPresenter ContentTemplate="{StaticResource EditingTemplate}"
                              Content="{Binding Path=(helpers:DataBindingHelper.AttachedBinding), RelativeSource={RelativeSource AncestorType={x:Type wpftk:DataGridTemplateColumn}}}" />                            
        </DataTemplate>

    </wpftk:DataGrid.Resources>

    <wpftk:DataGrid.Columns>
        <wpftk:DataGridTemplateColumn Header="Start Date" helpers:DataBindingHelper.AttachedBinding="{Binding Path=StartDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
        <wpftk:DataGridTemplateColumn Header="End Date" helpers:DataBindingHelper.AttachedBinding="{Binding Path=EndDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
    </wpftk:DataGrid.Columns>

</wpftk:DataGrid>

要了解有关附加属性的更多信息:请阅读MSDN 文档或任何 WPF/C# 书籍。它们是 WPF 中数据绑定系统最强大的功能之一。


此外,如果您想了解有关调试和迭代 WPF 应用程序的更多信息,请阅读我最近关于该主题的回答。Snoop 尤其有助于您了解上述问题所在。

于 2012-03-22T17:25:10.277 回答