0

データグリッド列で cell/celledit テンプレートを共有したい。

私はそれを行うソリューションを持っています ( 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 のデータ バインディング システムの最も強力な機能の 1 つです。


また、WPF アプリケーションのデバッグと反復について詳しく知りたい場合は、このトピックに関する私の最近の回答をお読みください。Snoop は、上記の何が問題なのかを理解するのに特に役立ちました。

于 2012-03-22T17:25:10.277 に答える