i replace the ContentPresenter
in the DataGridCell
's Template
with a TextBlock
an now i search for the correct Binding
to the content.
我用DataBlock替换DataGridCell模板中的ContentPresenter现在我搜索到内容的正确绑定。
The normal way is Text="{TemplateBinding Content}
for the TextBlock
- it doesn't work. Also Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content, Mode=TwoWay}"
doesn't work correct.
正常的方法是TextBlock的Text =“{TemplateBinding Content}” - 它不起作用。还有Text =“{Binding RelativeSource = {RelativeSource TemplatedParent},Path = Content,Mode = TwoWay}”“无法正常工作。
Any other ideas?
还有其他想法吗?
2 个解决方案
#1
12
Suppose you have changed the DataGridCell Template
to the following
假设您已将DataGridCell模板更改为以下内容
<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>
Since you removed the ContentPresenter
, the DataGridCell
has no way of displaying its Content
. It's still there though. The DataGridCell.Content
is a TextBlock
containing your original Text
and the TextBlock
in the Template
is another.
由于您删除了ContentPresenter,因此DataGridCell无法显示其内容。它仍然在那里。 DataGridCell.Content是一个包含原始Text的TextBlock,而Template中的TextBlock是另一个。
So you'll get the correct Text
by binding it to the Content.Text
property of the TemplatedParent
因此,您将通过将其绑定到TemplatedParent的Content.Text属性来获取正确的Text
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Content.Text}"/>
So, to sum it up. This works
所以,总结一下。这很有效
<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>
#2
2
The data context of the data grid cell should be the data itself. So the binding should simply be:
数据网格单元的数据上下文应该是数据本身。所以绑定应该只是:
<TextBlock Text="{Binding}"/>
#1
12
Suppose you have changed the DataGridCell Template
to the following
假设您已将DataGridCell模板更改为以下内容
<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>
Since you removed the ContentPresenter
, the DataGridCell
has no way of displaying its Content
. It's still there though. The DataGridCell.Content
is a TextBlock
containing your original Text
and the TextBlock
in the Template
is another.
由于您删除了ContentPresenter,因此DataGridCell无法显示其内容。它仍然在那里。 DataGridCell.Content是一个包含原始Text的TextBlock,而Template中的TextBlock是另一个。
So you'll get the correct Text
by binding it to the Content.Text
property of the TemplatedParent
因此,您将通过将其绑定到TemplatedParent的Content.Text属性来获取正确的Text
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Content.Text}"/>
So, to sum it up. This works
所以,总结一下。这很有效
<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>
#2
2
The data context of the data grid cell should be the data itself. So the binding should simply be:
数据网格单元的数据上下文应该是数据本身。所以绑定应该只是:
<TextBlock Text="{Binding}"/>