2012-03-22 77 views
0

我想要我的datagrid列共享單元/ celledit模板。wpftoolkit DataGridTemplateColumn模板綁定

我有解決方案做到這一點(感謝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> 

顯然綁定porperty不是DataGridTemplateColumn的有效屬性,但也許用在DataContext和一些相對源播放可以做的伎倆,但坦率地說我無法找到實現這一點的方法。

不知道如果我想是可能的,我願意接受一個「沒有辦法,你可以做到這一點」作爲回答

注意:模板中的TextBlock/TextBox只是爲了測試(真正的模板要複雜得多)DataGridTextColumn不會做的伎倆 在此先感謝

回答

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尤其會幫助你理解上述問題。

+0

不知道我跟着你,我的問題是,我不知道如何直接從DataGridTemplateColumn綁定實際數據,綁定不是DataGridTemplateColumn的屬性。我可以綁定編輯模板和普通模板,但我不知道如何將數據綁定到模板。我的猜測是用我想要的數據設置模板的DataContext,但我不知道該怎麼做 – Guillaume 2012-03-23 10:12:15

+0

編輯答案以更好地回答您的原始問題。 – Dennis 2012-03-23 11:45:41

+0

太棒了!那是我在找的東西 – Guillaume 2012-03-23 11:56:14