如何在wpf中获取datagrid的值 [英] how get value of datagrid in wpf
问题描述
hi
i有一个带有一些行和culomns的数据网格和按钮发送来自
i想要在datagrid中选择一行并将所选行的值发送到另一种形式
怎么做这个????????
plase F1F1F1F1F1F1F1
hi
i have a datagrid with some rows and culomns and button send in from
i want a select row in datagrid and send values of selected row to another form
how do this????????
plase F1F1F1F1F1F1F1
推荐答案
让我们假设我们有这样的数据网格:
Let''s assume we have data grid like this:
<DataGrid Name="DataGrid1" SelectionMode="Single"
SelectionChanged="DataGrid1_SelectionChanged">
</DataGrid>
和代码隐藏中的事件处理程序(xaml.cs文件):
and event handler in code behind (xaml.cs file):
private void DataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = DataGrid1.SelectedItem/* as YourBusinessObject*/;
if (item != null)
{
// Your code here
}
}
您应该投射所选项目到您的业务对象类型。
如果您使用的是匿名类型 ,您可以结合使用 Selector.SelectedValuePath Property [ ^ ]和 Selector.SelectedValue属性 [ ^ ]。
You should cast selected item to type of your business object.
If you are using anonymous types, you can use combination of the Selector.SelectedValuePath Property[^] and Selector.SelectedValue Property[^].
如果你想在按钮点击中选择行,你可以在按钮点击中写下面的代码
If you want selected row in button click ,you can write the below code in button click
Yourbusinessobject object= this.DataGrid1.SelectedItem as Yourbusinessobject ;
和xaml代码中
and in xaml code put
SelectionMode="Single" and SelectionUnit="FullRow"
。
in datagrid.
这篇关于如何在wpf中获取datagrid的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!