从 Datagrid 单元格或行中获取文本 [英] Get Text from Datagrid cell or Row
本文介绍了从 Datagrid 单元格或行中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在使用数据网格在 WPF 中显示可观察集合.现在如何从 DataGrid 获取选定的行文本,以便我可以调用函数.这是我的 XAML:
Hello i am using the data grid to show an observable collection in WPF. Now how can i get the selected row text from the DataGrid so i can call a function. Here is my XAML:
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="0,77,0,0" VerticalAlignment="Top" Height="720" Width="664" ItemsSource="{Binding Items}" AutoGenerateColumns="False" Grid.ColumnSpan="2" SelectedCellsChanged="dataGrid_SelectedCellsChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="ProjectName" MinWidth="100" Binding="{Binding Path=ProjectName,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Header="Name" MinWidth="100" Binding="{Binding Path=Name,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Header="Path" MinWidth="460" Binding="{Binding Path=Path,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
如何从选定的单元格或行中获取文本?
How can i get the text from the selected cell or row?
推荐答案
如果你绑定的 Items
集合是一个 IEnumerable
,你可以将 Items
DataGrid
到 T
的 code>SelectedItem 属性:
If the Items
collection you bind to is an IEnumerable<T>
, you could cast the SelectedItem
property of the DataGrid
to T
:
YourItemType obj = dataGrid.SelectedItem as YourItemType;
string name = obj.Name;
或者您可以将 SelectedItem
属性绑定到 T
类型的属性,就像绑定 ItemsSource
属性一样:
Or you could bind the SelectedItem
property to a property of type T
, just as you bind the ItemsSource
property:
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ...>
这篇关于从 Datagrid 单元格或行中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文