2012-03-22 20 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レベルの値を結合して、テンプレートのレベルにそれを伝播することです。誰でもそのことを知っていますか?結合porpertyがDataGridTemplateColumnの有効なプロパティではありませんが、おそらくのDataContextといくつかの相対的なソースと一緒に遊んで率直にトリックを行うが、可能性が明らかに

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

:私はやってみました何

はそのような何かであります私はそれを実装する方法を見つけることができません。

ない私がしたいことは可能であると私は答えとして

NOTE「あなたがそれを行うことができる方法は、」受け入れることを望んだかどうかわから:テンプレートにTextBlock/TextBoxは単なるテストのためではありませんあなたは、プロパティ構文を使用してDataTemplatesを指定することによって、XAMLを低減して(実際のテンプレートがはるかに複雑である)DataGridTextColumnは、トリックを行うことはありません事前に おかげ

答えて

1

は、あなたがしようとした何を働いているはずです。

編集:申し訳ありません。私はあなたの質問を誤解しました。ソースコードを変更またはアクセスせずに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のデータバインディングシステムに関する最も強力な機能の1つです。


はまた、あなたは、デバッグの詳細を知りたいとWPFアプリケーションの反復トピックの私の最近のanswerを読めば。特にスヌープは、あなたが何が間違っているのかを理解するのに役立ちました。

+0

私はあなたに従っていますが、私の問題はDataGridTemplateColumnから実際のデータを直接バインドする方法がわからないことです.BindingはDataGridTemplateColumnのプロパティではありません。編集テンプレートと通常のテンプレートをバインドできますが、データをテンプレートにバインドする方法はわかりません。私の推測は、私が望むデータでテンプレートのDataContextを設定することですが、私はそれを行う方法がわかりません – Guillaume

+0

あなたの元の質問によく答えるために編集された答え。 – Dennis

+0

素晴らしい!私が探していたもの – Guillaume

関連する問題