C# DataContext 绑定和刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/301716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataContext Binding and Refresh
提问by
My scenario is:
我的场景是:
I have a WPF Window with 3 data-bound text boxes
我有一个带有 3 个数据绑定文本框的 WPF 窗口
SettingsUI : Window
<Grid Name="SettingsUIGrid1">
<TextBox Text="{Binding val1}" ....
<TextBox Text="{Binding val2}" ....
<TextBox Text="{Binding val3}" ....
</Grid>
In the constructor I do this:
在构造函数中,我这样做:
SettingsUIGrid1.DataContext = coll[0]; // collection first value
When the Cancel button is clicked, I close my window:
单击取消按钮后,我关闭窗口:
private void btnCancel_Click(object sender, RoutedEventArgs e) {
Close();
}
When I click the Show button, is shows values from the DB in text boxes, if user changes a text box value, and reloads the window the new value is displayed not the old one. Can someone suggest what to do to reload the values again and clear the in memory object?
当我单击“显示”按钮时,会在文本框中显示来自数据库的值,如果用户更改文本框值并重新加载窗口,则显示新值而不是旧值。有人可以建议如何重新加载值并清除内存对象吗?
回答by Bijington
I would suggest putting some code into your cancel button click event to check whether any of the data has been changed from what was initially loaded as I am assuming that if they don't click cancel the other button would be save/ok meaning that the data would be committed to the database.
我建议将一些代码放入您的取消按钮单击事件中,以检查是否有任何数据已从最初加载的数据中更改,因为我假设如果他们不单击取消,则其他按钮将保存/确定,这意味着数据将提交到数据库。
What is coll?
什么是科尔?
if it is a DataTable then you could use this:
如果它是一个 DataTable 那么你可以使用这个:
private static bool DataRowReallyChanged(DataRow row)
{
if (row == null)
{
return false;
}
if (!row.HasVersion(DataRowVersion.Current) || (row.RowState == DataRowState.Unchanged))
{
return false;
}
foreach (DataColumn c in row.Table.Columns)
{
if (row[c, DataRowVersion.Current].ToString() != row[c, DataRowVersion.Original].ToString())
{
return true;
}
}
return false;
}
then simply add a call into the cancel button event like:
然后只需在取消按钮事件中添加一个调用,例如:
if (DataRowReallyChanged((DataRow)SettingsUIGrid1.DataContext))
{
((DataRow)SettingsUIGrid1.DataContext).RejectChanges();
}
I hope this helps. If you are not using a DataTable then let me know what you are using and ill see if i can help further.
我希望这有帮助。如果您没有使用 DataTable,那么请告诉我您在使用什么,然后看看我是否可以提供进一步的帮助。
回答by Arcturus
The Binding works two way: it takes the value and sets it editable in the textbox, and if the value changes, it updates the original object's value...
绑定以两种方式工作:获取值并将其设置为可在文本框中编辑,如果值发生变化,它会更新原始对象的值...
If you only wish to show the data, you can use the Mode=OneTime option in the Binding
如果您只想显示数据,您可以使用 Binding 中的 Mode=OneTime 选项
{Binding Path =val1, Mode=OneTime}
This will only evaluate the data once..
这只会评估数据一次..
If you do need to modify the data, make sure you clone or use another object for your datasource property.. This way you can discard the datasource object if it is no longer needed...
如果您确实需要修改数据,请确保为您的数据源属性克隆或使用另一个对象。这样您就可以在不再需要时丢弃数据源对象...
HTH
HTH
回答by Bijington
Ok sorry just to clarify everything you are loading the collection from a database and populating your textboxes, the user can then modify these values which depending on whether they click save/ok or cancel will save or not save the changes respectively. if this is the case then I would use arcturus's solution.
好的抱歉只是为了澄清您从数据库加载集合并填充文本框的所有内容,然后用户可以修改这些值,这取决于他们是单击保存/确定还是取消将分别保存或不保存更改。如果是这种情况,那么我将使用 arcturus 的解决方案。
I used my solution as we created our own 'typed' DataSets which implemented IEnumerable to make life easier than having to keep populating Collections. Although I am starting to wish we used LINQ.
我使用了我的解决方案,因为我们创建了我们自己的“类型化”数据集,它实现了 IEnumerable,使生活比必须继续填充集合更容易。虽然我开始希望我们使用 LINQ。
回答by Bijington
I did it this way -> added a text box more, binded it with ID of the object (only one object should be there since I have only one record that stores all my settings), and used ,Mode=OneTime for all binding.....so if user saves it will update the existing object.Cancel will work now.....................whew!
我是这样做的 -> 再添加一个文本框,将它与对象的 ID 绑定(只有一个对象应该在那里,因为我只有一个存储我所有设置的记录),并使用 ,Mode=OneTime 进行所有绑定。 ....所以如果用户保存它会更新现有的对象。取消现在可以工作了........................哇!
回答by Sacha Bruttin
If you don't want to modify your data, you can set you binding to be OneWay:
如果不想修改数据,可以将绑定设置为 OneWay:
<Grid Name="SettingsUIGrid1">
<TextBox Text="{Binding Path =val1, Mode=OneWay}" ....
<TextBox Text="{Binding Path =val2, Mode=OneWay}" ....
<TextBox Text="{Binding Path =val3, Mode=OneWay}" ....
</Grid>