通过数据绑定从数据网格单元格中获取值 - wpf c#
Get value from a datagrid cell by Data Binding - wpf c#
我在尝试获取当前所选行的值时遇到了问题。我尝试了互联网上的示例,但它们没有用。喜欢:DataRowView drv = (DataRowView)clientList.SelectedItem;
我发现但想不通的是如何通过数据绑定获取价值。由于我是整个 .net 和 c# 编程的新手,所以我不明白如何完成我需要的东西。
基本上在我的 Test.xaml.cs 中,我需要获取 Id 列值以了解当前选定行的哪一行以在数据库中修改它。
<DataGridTextColumn Width="30" Header="Id" Binding="{Binding Id}"/>
已更新
public class DataClients
{
public int Id { get; set; }
public string Company { get; set; }
public string Name { get; set; }
}
看看绑定模式。这里稍微link给大家介绍一下。
https://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode%28v=vs.110%29.aspx
我遇到了同样的问题,刚刚解决了。
基本上,您的转换是错误的,我猜 Id 是 class 的 属性,所以您需要转换并捕获您的 class,而不是 DataRowView
Client selectedClient = (Client)clientList.SelectedItem;
// This will return the instance of the class that is selected.
之所以可行,是因为当您绑定时,网格上的每个项目实际上都与其整体的实例相关联 class,因此当您获得所选项目时,它会返回一个 "Client"(我猜你的 class 被称为类似的东西)装在一个对象中。
无论如何,这对我有用,希望对您有所帮助:)。
我在尝试获取当前所选行的值时遇到了问题。我尝试了互联网上的示例,但它们没有用。喜欢:DataRowView drv = (DataRowView)clientList.SelectedItem;
我发现但想不通的是如何通过数据绑定获取价值。由于我是整个 .net 和 c# 编程的新手,所以我不明白如何完成我需要的东西。 基本上在我的 Test.xaml.cs 中,我需要获取 Id 列值以了解当前选定行的哪一行以在数据库中修改它。
<DataGridTextColumn Width="30" Header="Id" Binding="{Binding Id}"/>
已更新
public class DataClients
{
public int Id { get; set; }
public string Company { get; set; }
public string Name { get; set; }
}
看看绑定模式。这里稍微link给大家介绍一下。
https://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode%28v=vs.110%29.aspx
我遇到了同样的问题,刚刚解决了。
基本上,您的转换是错误的,我猜 Id 是 class 的 属性,所以您需要转换并捕获您的 class,而不是 DataRowView
Client selectedClient = (Client)clientList.SelectedItem;
// This will return the instance of the class that is selected.
之所以可行,是因为当您绑定时,网格上的每个项目实际上都与其整体的实例相关联 class,因此当您获得所选项目时,它会返回一个 "Client"(我猜你的 class 被称为类似的东西)装在一个对象中。
无论如何,这对我有用,希望对您有所帮助:)。