古伯,
我不确定我是否会回答您的问题,但我将向您展示如何将信息绑定到代码中的DataGrid,并将该信息设置为双向绑定。
首先,link 讨论了单向、一次性和双向绑定之间的区别。
在数据网格中进行编辑后,您可以通过三种方式更改数据源:
第一
您可以手动设置 XAML 中的列,并指示与每个变量的绑定是“双向”的。
<data:DataGrid Name="data" AutoGenerateColumns="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Test"
Binding="{Binding test, Mode=Two-Way}"/>
</data:DataGrid.Columns>
</data:DataGrid>
第二
你可以在页面后面的代码中做同样的事情。
rpdata.ItemsSource = info;
rpdata.Columns.Clear();
DataGridTextColumn user = new DataGridTextColumn();
user.Header = "User";
user.Binding = new System.Windows.Data.Binding("User");
user.Binding.Mode = BindingMode.TwoWay;
rpdata.Columns.Add(user);
rpdata 表示数据网格。
第三
您可以将“CellEditEnded”事件添加到数据网格中,而不是手动链接每一列。
在事件的代码中,您可以添加以下代码:
xt.CommitEdit();
这会更新数据源。
您必须为您的服务创建一个新合同,该合同采用已由数据网格更新的实体。
[OperationContract]
public void UpdateWork(List<Assumptions> updates)
这里我的实体被称为“假设”。
在该操作合同中添加以下代码。
//Create a new entity datacontext
Entities ds = new Entities();
//For each of the entities in the list
foreach (Assumptions update in updates)
{
try
{
//In the datacontext find the entity with the same primary key as the updated one
var test = from x in ds.Assumptions
where x.ID.Equals(update.ID)
select x;
//Update the entity
test.First() = update;
}
catch (Exception e)
{
//If the entity does not exist, add it to the context
ds.AddToAssumptions(update);
}
}
//Save the context
ds.SaveChanges();