我有一个带有主/详细视图的 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)
我通过自定义此处发布的解决方案获得了此解决方案
任何人都可以帮我弄清楚为什么我的列表框在发生这种情况时会不同步。
提前致谢