MVVM实现的问题:对所选项目的更改要列出,不要这样做 [英] Problem with MVVM implementation: Changes to selected item propogate to list, do not want that

查看:207
本文介绍了MVVM实现的问题:对所选项目的更改要列出,不要这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个问题,希望你能帮助。

So I'm having this problem that hopefully you can help with.

我正在使用MVVM Light编写一个WPF应用程序作为框架。在这种情况下,我有一个项目列表,SelectedItem绑定到用户可以编辑项目的详细信息视图。在这种情况下有一个保存按钮用于显式保存数据。

I'm writing a WPF application using MVVM Light as a framework. In this situation, I have a list of items, and the SelectedItem is bound to a details view where the user can edit the item. There is a Save button in this case for explicit saving of data.

我的问题是,当用户编辑数据时,更改立即显示在列表中。如果用户取消,则会重置所选项,但仍会更改。我如何防止传播的变化?

My problem is that when the user edits the data, the changes immediately shows up in the list. If the user cancels, it resets the selected item, but it's still changed. How do I prevent changes from propogating?

我尝试实现克隆实现,但是一旦我这样做,MVVM Light的消息传递系统就会进入循环,导致StackOverflowException由于我不断克隆对象的事实。除此之外,克隆实现是丑陋的。

I tried to implement a cloning implementation, but as soon as I did that, MVVM Light's messaging system ends up getting into a loop, resulting in a StackOverflowException due to the fact that I keep cloning the object. As well, the clone implementation is ugly.

任何关于如何正确执行此操作的想法?

Any idea on how I can do this properly?

编辑:

列表视图的基本XAML:

Basic XAML for list view:

    <DataGrid DataContext="{Binding SubJobTypes}"
              ItemsSource="{Binding}"
              SelectedItem="{Binding ElementName=Root, Path=DataContext.SelectedSubJobType, Mode=TwoWay}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
        </DataGrid.Columns>
    </DataGrid>

编辑视图的基本XAML:

Basic XAML for edit view:

   <StackPanel>
        <StackPanel>
            <StackPanel Orientation="Horizontal" DataContext="{Binding Path=CurrentSubJobType}">
                <TextBlock Text="Name"/>
                <TextBox Text="{Binding Path=Name, Mode=TwoWay}" Width="150"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Button Content="{Binding Path=SubmitCommandText, FallbackValue=Submit}" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=SaveSubJobTypeCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
                <Button Content="Cancel" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=CancelCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
                <Button Content="Delete">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=DeleteCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </StackPanel>
        </StackPanel>
    </StackPanel>

ViewModels是标准的,不会打扰发布

ViewModels are standard, won't bother posting

推荐答案

而不是禁用绑定机制,您应该有两个视图模型,一个用于列表项,另一个用于详细视图。选择一个项目后,列表视图模型会发送一个已更改的属性的消息。细节视图模型然后加载项目数据,或克隆模型,并使用数据进行初始化。现在,详细视图模型可以改变其模型的本地实例。

Instead of disabling the binding mechanism you should have two view models, one for the list items and one for the detail view. Once the an item is selected the list view model sends a property changed changed message. The detail view model then loads the items data, or clones the model and intializes itself with the data. Now the detail view model can alter its local instance of the model.

完成数据后,数据将保存到数据库,详细信息视图模型会发送一条消息,表明该项目已更改。列表视图模型现在接收到消息,并且可以使用模型来更改其项目视图模型,或者可以从数据库重新加载项目模型,然后更新项目视图模型。

Once finished the data is saved to the database and the detail view model sends a message that the item has changed. The list view model now receives the message and can use the model to change its item view model, or you can reload the item model from the database and then update the item view model.

这样你就不需要手动将值写入模型了,如果你只是克隆模型,那么你不应该遇到消息传递的任何问题。

This way you do not have to mess around with manually writing the values to the model and if you only clone the model you should not run into any problems with the messaging.

这篇关于MVVM实现的问题:对所选项目的更改要列出,不要这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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