【问题标题】:Mvvm - Cancel changes in Wpf Listbox, vb.netMvvm - 取消 Wpf Listbox 中的更改,vb.net
【发布时间】:2013-01-25 06:54:31
【问题描述】:

我有一个带有主/详细视图的 wpv/mvvm-light/vb.net 应用程序。在此视图中,有一个客户列表框和客户详细信息的详细视图,用户可以在其中查看和编辑客户。

我想添加一个功能,当在列表框中选择新客户端时,系统会提示用户保存更改。如果用户从消息框中选择是,则保存更改,如果否,则丢弃更改并将先前选定的项目返回到其原始值。我有这一切工作正常。

我的问题是,当用户选择一个新客户端并且消息框要求他们保存更改时,列表框会不同步。这意味着列表框显示选择的新客户端,但详细视图仍显示以前的客户端。奇怪的是它在极少数情况下也能正常工作。

以下是我的看法:

<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>

以下是列表框的选定项绑定到的视图模型中的属性。它也是显示细节的内容控件的绑定。

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 并继续选择选择的新值由用户。然而,就像我上面所说的,即使我厌倦了用下面的代码对它进行硬编码,列表框也没有反映这种变化:

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

我通过自定义发布HERE的解决方案得到了这个解决方案

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

提前致谢

【问题讨论】:

    标签: wpf vb.net listview mvvm mvvm-light


    【解决方案1】:

    首先: 我在您的解决方案中找不到真正的问题,但是您的 Property Setter 中肯定有 - 我重复一遍 - 肯定有太多的代码和逻辑。尝试将其移至其他方法并验证您对那些“if else”块的实现。

    第二: 仅当您在列表框中选择一个新项目时,Setter 才会被触发,但是您为“ClientSelectedPropertyName”而不是“Selection”引发属性更改,因为它应该是。将始终更改的属性移动到 setter 的末尾。

    试试这个。希望对你有帮助:)

    【讨论】:

    • 感谢您的回复。我也对我的 setter 中的代码量感到不舒服。但我正在尝试遵循 MVVM 模式,因此 UI 视图需要与 ViewModel 分离。所以我不能在 view.xaml 对象的事件后面使用简单的代码。因此,我如何在视图模型代码中管理这一点,以及在选择变化时执行的唯一代码是选择对象的SETTER。所以我把取消那个改变的逻辑放在那里。你知道我可以做到这一点的另一种方法吗?
    【解决方案2】:

    所以我有一个我认为遵循 MVVM-Light 标准的工作示例。有很多事情要做,所以我会尽量保持简短和准确。

    我最终使用 EventToCommand 绑定到具有 ListView(而不是列表框)的 SelectionChanged 事件。 EventToCommand 需要新的命名空间引用,如下所示。然后,我将 EventToCommand 绑定到视图模型中的 RelayCommand,后者又调用一个私有子来处理客户端验证并保存/取消/并根据需要更新列表视图选择项。

    有关更多信息,我有一个导航服务,用于在我的 wpf 应用程序中的视图之间导航。我使用 MVVM-Light messanger 发送了一个由该视图模型“接收”的导航启动消息。然后执行相同的客户端验证功能,并根据用户对抛出的对话消息的响应取消/允许导航。除非要求,否则我不会包含所有 hte 导航代码。以下是解决我原来的问题所需的代码。

    <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>
    

    那么下面是我的视图模型中的相关代码(为了清楚起见,我删除了尽可能多的代码):

    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,您会注意到选择属性设置器的代码/逻辑更少。另外,我认为视图模型的每个部分都是可测试的,并且我的视图和视图模型之间没有直接耦合。但这是我的第一个 WPF/MVVM 应用程序,所以我不会完全掌握所有概念。

    我希望这可以帮助某人,因为我花了很长时间才弄清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      相关资源
      最近更新 更多