DataGridTemplateColumn单元格的WPF内容 [英] WPF contents of cell of DataGridTemplateColumn

查看:394
本文介绍了DataGridTemplateColumn单元格的WPF内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,我引用了DataGridCell,并希望获取其内容。我以前在DataGridTextColumn中有该单元格,并且可以得到如下内容:

In WPF I have the reference to a DataGridCell and would like to get its contents. I used to have that cell in a DataGridTextColumn and could get at the content like this:

var text = cell.Content as TextBlock;

但这不再起作用,因为单元格位于DataGridTemplateColumn中,尽管我确实将TextBlock指定为该列的DataTemplate。

But this is not longer working since the cell is in a DataGridTemplateColumn, although I did specify TextBlock as the DataTemplate for that column. Is there still a way to get at it?

编辑以澄清问题。以下代码按预期工作:

EDIT to clarify the problem. The following code is working as intended:

<!-- XAML -->
<DataGridTextColumn Header="Autor" Width="*"  Binding="{Binding Author}" />

//C#
var block = _selectedCell.Content as TextBlock;
var text = block.Text; //text contains the string that is also displayed by the grid in that call

中也显示的字符串TemplateColumn该代码将不起作用,因为block为空。

If I however use a TemplateColumn the code will not work because block will be null.

<DataGridTemplateColumn Header="Autor" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Name="txtAutor" Text="{Binding Author}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

有没有办法获取单元格中的内容(在我的情况下为字符串)?

Is there a way to still get at the cells contents (a string in my case)?

推荐答案

您应该可以将 TextBlock 放在 DataTemplate 一个名称,然后使用 Text 属性获取数据。

You should be able to give your TextBlock inside the DataTemplate a name, and then use the Text property to get the data.

<DataTemplate>
    <TextBlock Name="txtData" Text="{Binding}" />
</DataTemplate>

var text = txtData.Text as string;

这篇关于DataGridTemplateColumn单元格的WPF内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆