MVVM - 取消在WPF列表框,vb.net变化 [英] Mvvm - Cancel changes in Wpf Listbox, vb.net

查看:468
本文介绍了MVVM - 取消在WPF列表框,vb.net变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主/详细信息视图一个WPV / MVVM光/ vb.net应用。根据这种观点有客户的一个列表框和客户的详细信息的详细视图,他们的用户可以查看和编辑的客户。

I have a wpv/mvvm-light/vb.net application with a master/detail view. In this view there is a listbox of clients and a detail view of the client's details where they user can view and edit the customers.

我想添加,用户将被提示时,在列表框中选择一个新的客户端保存更改的功能。如果用户从消息框是选择,然后保存更改,如果没有再放弃更改并返回previous所选项目恢复到原来的值。我有这一切工作的罚款。

I wanted to add a function where users would be prompted to save changes when a new client is selected in the listbox. If the user chooses yes from the messagebox then save changes and if no then discard changes and return previous selected item back to its original value. I have this all working fine.

我的问题是,当用户选择一个新的客户端和消息框要求他们保存更改,列表框变为不同步。这意味着列表框显示所选择的新的客户端,但详细视图仍然显示previous客户端。奇怪的是,它正常工作在罕见的情况。

My problem is that when the user selects a new client and the messagebox asks them to save changes, the listbox goes out of sync. Meaning that the listbox shows the new client selected but the detail view still shows the previous client. The odd thing is that it works properly on rare occasions.

以下是我的观点:

<UserControl x:Class="FTC.View.ClientListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:FTC_Application"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="900">


                <ListBox    
                    Grid.Column="1" 
                    Width="350"                    
                    Style="{DynamicResource FTC_ListBox}"  
                    ItemTemplate="{DynamicResource FTC_ClientListTemplate}" 
                    ItemContainerStyle="{DynamicResource FTC_ListItem}"
                                ItemsSource="{Binding ClientViewSource.View}" 
                                SelectedItem="{Binding Path=Selection, Mode=TwoWay}"                
                    />


                    <ContentControl DataContext="{Binding Path=Selection, Mode=TwoWay}" >
                        <!--all the display stuff goes here for the detail view-->
                    </ContentControl>

</UserControl>

以下是该列表框的selectedItem属性绑定到视图模型的财产。它也为显示的细节内容对照结合

the following is the property in the viewmodel that the selecteditem of the listbox is bound to. It is also the binding for the content control that displays the details.

Public Property Selection As client
            Get
                Return Me._Selection
            End Get
            Set(ByVal value As client)
                ''capture current value of selection
                _PreviousClient = _Selection

                ''If they are the same, 
                If value Is _PreviousClient Then
                    Return
                End If

                ' Note that we actually change the value for now.This is necessary because WPF seems to query the
                '  value after the change. The list box likes to know that the value did change.
                If Me._Selection.HasChanges = True And _Selection.HasErrors = False Then
                    'If HasChangesPrompt(value) = True Then
                    '    ''user rejects saving changes, exit property
                    '    Return
                    'End If
                    If FTCMessageBox.Show("Do you want to save your changes", "Unsaved Changes", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
                        ''SELECTION IS CANCELLED
                        ' change the value back, but do so after the  UI has finished it's current context operation.
                        Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
                                                                                  '' revert the current selected item to its original values and reset its HasCHanges tracking
                                                                                  objHelper.CopyProperties(_OriginalClient, _Selection)
                                                                                  _Selection.HasChanges = False
                                                                                  RaisePropertyChanged(ClientSelectedPropertyName)
                                                                                  ''continue with listbox selection changing to the new value for selection
                                                                                  _ClientCollectionViewSource.View.MoveCurrentTo(value)
                                                                              End Sub), DispatcherPriority.Normal, Nothing)
                        Return
                    Else
                        ''save changes to database
                        SaveExecute()
                    End If
                End If

                _Selection = value

                _Selection.HasChanges = False
                RaisePropertyChanged(ClientSelectedPropertyName)

                ''clone the unchanged version of the current selected client on na original variable
                objHelper.CopyProperties(_Selection, _OriginalClient)

            End Set
        End Property

这样的想法是,如果用户不希望保存的改变,客户端的原始值被复制(用反射)上的当前值,则该UI被更新,并且在选择继续到选择的新值由用户。然而,像我上面所说的,列表框不反映此更改,即使我累了硬code它与以下行:

SO the idea is that if the user does not want to save changes, an original value of the client is copied (using reflection) over the current value, then the ui is updated and the selection continues on to the new value chosen by the user. However, like I said above, the listbox does not reflect this change even though I tired to hard code it with the following line:

''continue with listbox selection changing to the new value for selection  
 _ClientCollectionViewSource.View.MoveCurrentTo(value)

我靠打工定制发布解决方案<一个得到这个解决方案href=\"http://blog.alner.net/archive/2010/04/25/cancelling-selection-change-in-a-bound-wpf-combo-box.aspx\"相对=nofollow>这里

谁能帮助我弄清楚为什么我的列表框变为不同步时,发生这种情况。

can anyone help me figure out why my listbox goes out of sync when this happens.

在此先感谢

推荐答案

所以我有一个工作的例子,我认为后面的MVVM光强的标准。这里是怎么回事了很多,所以我会尽量保持短期和precise。

So I have a working example that I think follows the MVVM-Light standard. There is a lot going on so I will try to keep it short and precise.

我结束了使用绑定到SelectionChanged事件有一个ListView(而不是列表框)EventToCommand。的EventToCommand需要新的命名空间引用作为如下所示。然后我绑定的EventToCommand在其中依次调用处理客户端验证和保存/取消私有子/并更新所需的列表视图seleceditem视图模型RelayCommand。

I ended up using EventToCommand bound to the SelectionChanged event with a ListView(instead of listbox). The EventToCommand required new namespace references as is shown below. I then bound the EventToCommand to a RelayCommand in the view model which in turn calls a private sub that handles the client validation and saves/cancels/ and updates the listview seleceditem as required.

有关详细信息,我有一个使用的视图在我的WPF应用程序之间NAVIGAT导航服务。我用MVVM光强信使送由该视图模型收到一个navigationstarting消息。然后在同一客户机的验证功能被执行,并且naviagtion是根据用户响应于引发的对话消息取消/允许的。我不会包括所有兴田导航code,除非请求。以下是解决我原来的问题需要code。

For further information, I have a navigation service that is used to navigat between views in my wpf application. I used the MVVM-Light messanger to send a navigationstarting message that is "recieved" by this view model. Then the same client validation functions are performed and naviagtion is canceled/allowed based on user response to the dialog message thrown. I will no include all of hte navigation code unless requested. The following is the code needed to solve my original question.

<UserControl x:Class="FTC.View.ClientListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:FTC_Application"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:cmd="http://www.galasoft.ch/mvvmlight"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="900">

               <ListView    
                    Grid.Column="1" 
                    Width="350"                    
                    Style="{DynamicResource FTC_ListView}"  
                    ItemTemplate="{DynamicResource FTC_ClientListTemplate}" 
                    ItemContainerStyle="{DynamicResource FTC_ListViewItem}"
                    ItemsSource="{Binding ClientViewSource.View}" 
                    SelectedItem="{Binding Path=Selection, Mode=TwoWay}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <cmd:EventToCommand Command="{Binding SelectedItemChangedCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ListView> 

                   <ContentControl DataContext="{Binding Path=Selection, Mode=TwoWay}" >
                        <!-- Display stuff and bound controls go here -->
                    </ContentControl>


    </Grid>
</UserControl>

再下面是相关code(我删除尽可能多code尽可能保持清晰)在我的视图模型:

Then the following is the relevant code (I removed as much code as possible to keep it clear) in my view model:

Imports System.Data
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Windows.Threading

Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.Command
Imports GalaSoft.MvvmLight.Messaging

Imports FTCModel
Imports FTC_Application.FTC.Model
Imports FTC_Application.FTC.View
Imports FTC_Application.FTC.ViewModel
Imports FTC_Application.FTC.MessageBox
Imports FTC_Application.FTC.Helpers
Imports FTC_Application.FTC.MessengerHelper

Namespace FTC.ViewModel
    Public Class ClientListViewModel
        Inherits ViewModelBase
        Implements IDataErrorInfo

#Region "DECLARATIONS"

        Public Const ClientCollectionPropertyName As String = "ClientCollection"
        Public Const ClientSelectedPropertyName As String = "Selection"
        Public Const ClientDetailCollectionPropertyName As String = "ClientDetailCollection"
        Public Const ClientPropertyName As String = "Client"

        ''gets the data from LINQ to ENT Model
        Private _Clients As New ObservableCollection(Of client)
        ''creats holder for the selected item two way binding
        Private _Selection As New client
        ''the following is used to track changes for unding and canceling selection changed
        Private _PreviousClient As New client
        Private _PreviousOriginalClient As New client
        Private _OriginalClient As New client

        ''Recieves observable collection and provicdes sorting and filtering function
        Private _ClientCollectionViewSource As New CollectionViewSource

        ''RELAY COMMANDS declarations
        Private _SaveCommand As RelayCommand
        Private _SelectedItemChangedCommand As RelayCommand

        ''gets the VML for getting the data service
        Private vml As ViewModelLocator = TryCast(Application.Current.Resources("Locator"), ViewModelLocator)
        ''this is a holder for the client data service
        Private _ClientAccess As IClientDataService = vml.Client_Service

        '' has functions using reflection for copying objects
        Dim objHelper As New ObjectHelper

        ''tracks if client validation is coming from navigation or listview selecteditemchanged
        Private bNavigatingFlag As Boolean = False

#End Region

#Region "PROPERTIES"

        Public ReadOnly Property ClientViewSource As CollectionViewSource
            Get
                Return Me._ClientCollectionViewSource
            End Get
        End Property
        Private Property Clients As ObservableCollection(Of client)
            Get
                Return Me._Clients
            End Get
            Set(ByVal value As ObservableCollection(Of client))
                Me._Clients = value

                _Clients = value
                RaisePropertyChanged(ClientCollectionPropertyName)

            End Set
        End Property
        Public Property Selection As client
            Get
                Return Me._Selection
            End Get
            Set(ByVal value As client)
                ''capture current value of selection
                _PreviousClient = _Selection
                objHelper.CopyProperties(_OriginalClient, _PreviousOriginalClient)

                ''If they are the same, 
                If value Is _PreviousClient Then
                    Return
                End If

                _Selection = value
                _Selection.HasChanges = False
                RaisePropertyChanged(ClientSelectedPropertyName)
                ''clone the unchanged version of the current selected client on na original variable
                objHelper.CopyProperties(_Selection, _OriginalClient)

            End Set
        End Property

#End Region

#Region "COMMANDS"

        Public ReadOnly Property SelectedItemChangedCommand() As RelayCommand
            Get
                If _SelectedItemChangedCommand Is Nothing Then
                    _SelectedItemChangedCommand = New RelayCommand(AddressOf SelectionChangedValidate)
                End If
                Return _SelectedItemChangedCommand
            End Get
        End Property

#End Region

#Region "METHODS"

        Private Sub SelectionChangedValidate()

            ''Uses falg to tell if validation request triggered by navigation event or listview selecteditemchanged event
            ''use previous client for listview event and current client for navigating event
            Dim _ClientToValidate As client
            If bNavigatingFlag = True Then
                _ClientToValidate = _Selection
            Else
                _ClientToValidate = _PreviousClient
            End If

            If _ClientToValidate.HasChanges = True And _ClientToValidate.HasErrors = False Then
                Dim message = New DialogMessage(_ClientToValidate.chrCompany.ToString + " has been changed." + vbCrLf + "Do you want to save your changes?", AddressOf SavePreviousResponse) With { _
                     .Button = MessageBoxButton.YesNo, _
                     .Caption = "Unsaved Changes" _
                }
                Messenger.[Default].Send(message)
                Exit Sub
            End If

            If _ClientToValidate.HasErrors = True Then
                Dim message = New DialogMessage(_ClientToValidate.chrCompany.ToString + " has errors." + vbCrLf + "You must correct these errors before you can continue.", AddressOf HasErrorsResponse) With { _
                     .Button = MessageBoxButton.OK, _
                     .Caption = "Validation Error" _
                }
                Messenger.[Default].Send(message)
                Exit Sub
            End If

            ''reset the navigation flag
            bNavigatingFlag = False

        End Sub
        Private Sub SavePreviousResponse(result As MessageBoxResult)
            If result = MessageBoxResult.No Then
                objHelper.CopyProperties(_PreviousOriginalClient, _PreviousClient)
                _PreviousClient.HasChanges = False
            Else
                ''user wants to save changes, save changes to database
                SaveExecute()
            End If
        End Sub
        Private Sub HasErrorsResponse(result As MessageBoxResult)
            Selection = _PreviousClient
            ''_ClientCollectionViewSource.View.MoveCurrentTo(_PreviousClient)
        End Sub
        Private Function HasChangesPrompt(value As client) As Boolean
            If FTCMessageBox.Show("Do you want to save your changes", "Unsaved Changes", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
                '' change the selected client back to its original value, but do so after the  UI has finished its current context operation.
                Application.Current.Dispatcher.BeginInvoke(New Action(Sub()
                                                                          '' revert the current selected item to its original values and reset its HasCHanges tracking
                                                                          objHelper.CopyProperties(_OriginalClient, _Selection)
                                                                          _Selection.HasChanges = False
                                                                          RaisePropertyChanged(ClientSelectedPropertyName)
                                                                          ''continue with listbox selection changing to the new value for selection
                                                                          _ClientCollectionViewSource.View.MoveCurrentTo(value)
                                                                      End Sub), DispatcherPriority.Normal, Nothing)
                Return True
            Else
                ''user wants to save changes, save changes to database
                Return False
                SaveExecute()
            End If
        End Function

#End Region

        Public Sub New()

            Clients = _ClientAccess.GetClient_All

            ''Sets the observable collection as the source of the CollectionViewSource
            _ClientCollectionViewSource.Source = Clients

            If Selection.idClient = 0 Then
                Selection = Clients.Item(0)
            End If

            ''register for messages
            Messenger.[Default].Register(Of String)(Me, AddressOf HandleMessage)

        End Sub

    End Class

End Namespace

INXS,你会发现,选择属性setter方法​​有少code /逻辑。另外,我觉得视图模型的各个部分是可测试并有我的观点和视图模型之间没有直接耦合。但是,这是我的第一个WPF / MVVM应用程序,所以我罗纳尔多,也很想不完全掌握所有概念。

INXS, you will notice that the selection property setter has way less code/logic. Also, I think each part of the view model is testable and there is no direct coupling between my view and viewmodel. But this is my first WPF/MVVM application so I sitll don't fully grasp all the concepts.

我希望这可以帮助别人,因为我花了相当长一段时间来弄明白。

I hope that this can help someone as it took my quite a while to figure it out.

这篇关于MVVM - 取消在WPF列表框,vb.net变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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