4

のをに置き換えてContentPresenter、コンテンツに適したものを検索します。DataGridCellTemplateTextBlockBinding

通常の方法はText="{TemplateBinding Content}-TextBlock動作しません。またText="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content, Mode=TwoWay}"、正しく動作しません。

他のアイデアはありますか?

4

2 に答える 2

15

DataGridCell Templateを次のように変更したとします。

<ControlTemplate TargetType="{x:Type DataGridCell}">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <TextBlock Text="{Binding}"/>
        <!--<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> -->
    </Border>
</ControlTemplate>

を削除したためContentPresenter、 にDataGridCellは を表示する方法がありませんContent。それはまだそこにあります。はオリジナルを含むDataGridCell.Content で、は別のものです。TextBlockTextTextBlockTemplate

したがって、のプロパティにTextバインドすることで正しいものを取得できますContent.TextTemplatedParent

<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},
                          Path=Content.Text}"/>

ということで、まとめます。これは機能します

<ControlTemplate TargetType="{x:Type DataGridCell}">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                  Path=Content.Text}"/>
    </Border>
</ControlTemplate>
于 2011-08-23T13:28:01.650 に答える
1

データ グリッド セルのデータ コンテキストは、データ自体である必要があります。したがって、バインディングは次のようになります。

<TextBlock Text="{Binding}"/>
于 2011-08-23T12:48:21.857 に答える