如何使用Ok / Cancel按钮设计数据绑定WPF对话框的后台数据类型? [英] How do I design backing data types for a databound WPF dialog with Ok/Cancel buttons?

查看:473
本文介绍了如何使用Ok / Cancel按钮设计数据绑定WPF对话框的后台数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Microsoft Sql CE中创建了一个表,用于保存一些数据。我已经通过SqlMetal生成了一个ORM,我已经将数据源拉入我的WPF项目。

I've created a table in Microsoft Sql CE that I'm using to hold some data. I've generated an ORM via SqlMetal and I've pulled the datasource into my WPF project.

我想创建一个可以用来编辑的简单的WPF表单通过ORM生成的类记录在表中。我希望这个表单支持典型的OK / Cancel语义。我创建了我认为是典型时尚的表单,在相应的字段上使用TwoWay数据绑定与ORM中的对象实例绑定。例如,给定一个ORM中有一个属性TaskName的对象,我已经在我的WPF表单中包含了以下内容:

I want to create a simple WPF form that can be used to edit a record in the table via the ORM-generated classes. I want this form to support typical OK/Cancel semantics. I've created the form in what I believe to be the typical fashion, using TwoWay databinding on the respective fields to bind against an instance of the object from the ORM. For example, given an object in the ORM that has a property "TaskName", I've included the following in my WPF form:

<Label Grid.Column="0" Grid.Row="0" >
    Name:
</Label>
<TextBox Name="txtName" Grid.Column="1" Grid.Row="0" 
         Text="{Binding TaskName, Mode=TwoWay}" AcceptsReturn="False"
         MaxLines="1" />

这与代码中的DataContext分配相结合:

This, combined with a DataContext assignment in code:

var newRow = new OrmGeneratedClass();
// Populate default values on newRow, e.g.    
detailWindow.DataContext = newRow;
detailWindow.ShowDialog();

可以非常适合创建新行。通过表单进行的所有更改都会立即反映在底层的OrmGeneratedClass中。问题在于,如果OrmGeneratedClass填充了以前保存的值,则不需要立即支持取消更改。

can work reasonably well for creation of a new row. All changes made via the form are immediately reflected in the underlying OrmGeneratedClass. The problem is that there's no immediate support for canceling changes if, e.g., the OrmGeneratedClass is filled with previously saved values.

这种场景是什么好的设计,或者我在设计这个问题之前呢?我是WPF和Sql Server数据源/ ORM集成方面的新手。 (这是一个个人项目,我用来学习这两种技术。)

What is a good design for this scenario, or am I designing it wrong before I get to this point? I'm a newbie with WPF and the Sql Server datasource/ORM integration aspects. (This is a personal project that I'm using to learn both technologies.)

我有几个想法,并将他们放在答案中

I have a couple of thoughts and will place them in the answers

推荐答案

Paul Stovell有一篇关于为IEditableObject接口创建适配器的精彩文章,允许您进行数据绑定并允许应用/恢复行为。

Paul Stovell has a brilliant article about creating an adapter for the IEditableObject interface that allows you to databind and allow apply/revert behaviour.

http:// www.paulstovell.com/blog/runtime-ui-binding-behavior-ieditableobject-adapter

适配器包裹在您的ORM对象上,并自动缓存更改您可以应用更改或恢复原始。

The adapter wraps around your ORM object and automatically caches changes so you can either apply the changes or revert back to the original.

这篇关于如何使用Ok / Cancel按钮设计数据绑定WPF对话框的后台数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆