Silverlight DataForm、MVVM、IEditable 对象和自定义 EditTemplate。如何进行自定义脏状态检查

发布于 2024-12-01 11:28:54 字数 967 浏览 3 评论 0原文

我对 Silverlight DataForm 和子集合有一个有趣的问题。我也有一个可行的解决方案,但感觉像是一个黑客(它一个黑客),我想知道是否有人有更优雅的解决方案。

我有一个绑定到 GroupViewModel 集合的 DataForm。每个 GroupViewModel 都有一个 UserViewModels 的 ObservableCollection,我希望用户能够在显示 DataForm 的 EditTemplate 时添加和删除它。

用于执行此操作的 UI 是一个简单的组合框,其中包含所有用户的列表和当前组中用户的列表框。有一个添加和删除按钮可将用户从一个列表移动到另一个列表。

现在,...DataForm 绑定到的视图模型实现了 IEditableObject,...这很酷,因为如果取消编辑,我会得到回滚行为,但在这种情况下还不够。我需要一种方法来明确告诉 DataForm 我的虚拟机是否处于脏状态。

由于我的 ViewModel 有一个 SelectedUserToAdd 属性,所有用户 ComboBox 的 SelectedItem 都绑定到该属性,因此当用户选择要添加的用户时,PropertyChanged 事件会在我的 VM 上触发,并且 DataForm 认为某些内容已更改并单击提交(确定)按钮亮起。 由于 SelectedUserToRemove 属性,当列表框中的选择发生更改时,它也会亮起。

显然,我想要的是如果列表框中的 UserViewModel 发生更改,提交按钮就会亮起。

解决方案很简单。如果我从 SelectedUserToAdd 和 SelectedUserToRemove 属性中删除更改通知,我可以停止提交按钮亮起,并且可以获得 commit 按钮通过使用名为“Dirty”的虚拟属性来点亮备份,当 AddUser 或 DeleteUser 命令被触发时,该属性会引发 NotifyPropertyChanged。

正如我所说,..它有效,但感觉不对。有人有更好的主意吗?

非常感谢。

I have an interesting problem with the Silverlight DataForm and child collections. I've also got a solution that works but it feels like a hack (it is a hack) and I was wondering if anyone had a more elegant solution.

I've got a DataForm bound to a collection of GroupViewModels. Each GroupViewModel has a ObservableCollection of UserViewModels which I'd like the user to be able to add to and delete from when the EditTemplate of the DataForm is shown.

The UI for doing that is a simple ComboBox with a list of all Users and a ListBox of the users currently in the group. There is an add and a delete button to move a User from one list to the other.

Now,...the view model that the DataForm is bound to implements IEditableObject,..which is cool because I get rollback behaviour if an edit is cancelled but in this case it's not quite enough. I need a way of telling the DataForm whether my VM is in a dirty state or not explicitly.

Since my ViewModel has a SelectedUserToAdd property which the SelectedItem of the all users ComboBox is bound to, when the user selects a user to add, the PropertyChanged event fires on my VM and the DataForm thinks that something has changed and the commit (OK) button lights up.
It also lights up when the selection is changed in the ListBox because of the SelectedUserToRemove property.

Obviously what I want is for the commit button to light up if the UserViewModels in the ListBox have changed.

The solution is straight forward. I can stop the commit button lighting up if I remove change notification from the SelectedUserToAdd and SelectedUserToRemove properties and I can get the
commit button to light back up by using a dummy property called 'Dirty' which raises NotifyPropertyChanged when either the AddUser or DeleteUser command gets fired.

As I say,..it works but it feels wrong. Anyone got a better idea?

Much thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-12-08 11:28:54

根据我的经验,我知道 DataForm 使用起来并不理想,并且有一些限制。话虽这么说,我认为你的解决方案比我提出的解决方案更清晰,但我只是给你一个替代方案。

我可以告诉您一种使数据表单“忽略”(不是真正)某些属性通知的方法。您可以做的是检查是否引发了某个属性,如果应该忽略该属性,您可以手动忽略将提交按钮重置为其之前的状态:

        someViewModel.PropertyChanged += (sender, e) =>
                                             {
                                                 Grid dependencyObject = VisualTreeHelper.GetChild(TheDataForm, 0) as Grid;
                                                 var commitButton = dependencyObject.FindName("CommitButton") as Button;
                                                 commitButton.IsEnabled = !e.PropertyName.Equals("PropertyToIgnore");
                                             };

然后您可以通过从数据表单继承并创建一个可以在其中绑定的依赖属性来调整它要忽略的属性。

正如我所说,我认为您的解决方案是更干净的解决方案。

From my experience I know the DataForm isn't idealistic to use and has some limitations. This being said, I think your solution is cleaner then the one I'm proposing but I'm just giving you an alternative.

I can tell you of a way to make the dataform 'ignore' (not really) certain property notifications. What you could do is check whether a certain property is raised and if that property should be ignored you manually ignore reset the commit button to its previous state:

        someViewModel.PropertyChanged += (sender, e) =>
                                             {
                                                 Grid dependencyObject = VisualTreeHelper.GetChild(TheDataForm, 0) as Grid;
                                                 var commitButton = dependencyObject.FindName("CommitButton") as Button;
                                                 commitButton.IsEnabled = !e.PropertyName.Equals("PropertyToIgnore");
                                             };

You can then tweak it by inheriting from the dataform and creating a dependencyproperty in which you can bind the properties to ignore.

Again as I said, I think your solution is the cleaner one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文