【问题标题】:wpftoolkit DataGridTemplateColumn Template bindingwpftoolkit DataGridTemplateColumn 模板绑定
【发布时间】:2012-04-07 06:39:37
【问题描述】:

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

我有解决方案(感谢WPF DataGridTemplateColumn shared template?)。现在我希望通过避免所有节点嵌套来提高可读性。

我当前的视图是这样的:

  <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 不会起作用 提前致谢

【问题讨论】:

    标签: wpf xaml wpfdatagrid datagridtemplatecolumn


    【解决方案1】:

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

    编辑:抱歉。我误解了你的问题。要向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 documentation 或任何 WPF/C# 书籍。它们是 WPF 中数据绑定系统最强大的功能之一。


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

    【讨论】:

    • 不确定我在关注你,我的问题是我不知道如何直接从DataGridTemplateColumn绑定实际数据,绑定不是DataGridTemplateColumn的属性。我可以绑定编辑模板和普通模板,但我不知道如何将数据绑定到模板。我的猜测是用我想要的数据设置模板的 DataContext 但我不知道该怎么做
    • 编辑答案以更好地回答您的原始问题。
    • 太棒了!这就是我一直在寻找的
    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 2017-01-16
    • 2011-04-06
    • 2014-04-14
    • 2010-12-08
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多