在对象绑定 WPF 之前触发命令
本文关键字:命令 对象 绑定 WPF | 更新日期: 2023-09-27 18:36:59
我正在使用 Telerik 控件开发具有MVVM
模式的 WPF
应用程序。
功能性:
我正在使用根据记录数在运行时生成的telerik:RadListBox
。因此,如果我的收藏中有 10 条记录,则应用程序中将显示 10 条RadListBox
。当我选择每个RadListBox
时,SelectedItem
的详细视图(相关值)将显示在附近的面板中。一次只能选择一个RadListBox
。
场景:
因此,在选择RadListBox
并在面板中编辑相关信息后,当我切换到另一个RadListBox
时,将抛出警报(是/否)"您要保存详细信息吗?我已经在每个对象中实现了INPC
,我将检查它是否已更改。
/XAML:
<telerik:RadListBox x:Name="lstSeries" BorderThickness="1" BorderBrush="#FFCBD8E8" ItemsSource="{Binding SCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, ValidatesOnDataErrors=True}" ItemTemplate="{StaticResource ImageDataTemplate}" DragEnter="lstMarketSeries_DragEnter" DragLeave="lstMarketSeries_DragLeave" Style="{StaticResource myListboxStyle}" SelectedItem="{Binding SelectedSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" telerik:StyleManager.Theme="Windows8" PreviewKeyDown="RadListBox_PreviewKeyDown" MouseDoubleClick="lstMarketSeries_MouseDoubleClick" PreviewMouseDown="RadListBox_PreviewMouseLeftButtonDown" SelectionChanged="SeriesCommit_SelectionChanged">
</telerik:RadListBox>
键对象:
SelectedItem="{Binding SelectedSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
//视图模型:
/// <summary>
/// Get or set selected series.
/// </summary>
public SeriesBO SelectedSeries
{
get { return this.m_SelectedSeries; }
set
{
if (this.m_SelectedSeries != value)
{
this.m_SelectedSeries = value;
OnPropertyChanged();
}
}
}
//Method called once a RadListBox is selected
private void LoadSelectedMarketSeriesDetails()
{
if (!IsChanged())
{
//If there is no object edited then it should load the new selected object
}
}
private bool IsChanged()
{
bool IsChanged = false;
if (SeriesImageList != null)
IsChanged = IsChanged || SeriesImageList.Where(x => x.IsChanged || x.IsNew).Count() > 0;
if (NoteList != null)
IsChanged = IsChanged || NoteList.Where(x => x.IsChanged || x.IsNew).Count() > 0 || IsChanged;
if (IsChanged)
{
if (ShowMessages.SaveMessageBox())
{
//Hitting Yes in alert should save the values.
//When retrieving the SelectedSeries object it shows the recent object selected but i need the last selected one.
}
else
{
//Discard function
}
}
//After a successfull save or discard false is returned
return false;
}
现在的问题是,在编辑系列并切换警报(是/否)时,会抛出警报。然后我点击保存,但第二个对象被选中并绑定到SelectedSeries
对象中。当我尝试保存编辑的最后一个对象时,我无法获取这些值。
在绑定SelectedSeries
对象之前,我需要一些Command
来触发。这样我就可以检查属性是否已更改,并且我必须将绑定的对象限制为所选的第二个值。完成对前一个值的保存后,必须将SelectedSeries
绑定到对象。
预期成果:
编辑第一个选定的系列并将其切换到下一个系列后,应引发警报,并且警报的结果应保存或丢弃该系列,并应移动到所选的下一个系列。
尝试做下一件事:
已编辑的虚拟机
/// <summary>
/// Get or set selected series.
/// </summary>
public SeriesBO SelectedSeries
{
get { return this.m_SelectedSeries; }
set
{
if (this.m_SelectedSeries != value)
{
m_PrevSelectedSeries = m_SelectedSeries;
this.m_SelectedSeries = value;
OnPropertyChanged();
}
}
}
//Method called once a RadListBox is selected
private void LoadSelectedMarketSeriesDetails()
{
if (!IsChanged())
{
//If there is no object edited then it should load the new selected object
}
}
private bool IsChanged()
{
bool IsChanged = false;
if (SeriesImageList != null)
IsChanged = IsChanged || SeriesImageList.Where(x => x.IsChanged || x.IsNew).Count() > 0;
if (NoteList != null)
IsChanged = IsChanged || NoteList.Where(x => x.IsChanged || x.IsNew).Count() > 0 || IsChanged;
if (IsChanged)
{
if (ShowMessages.SaveMessageBox())
{
//Hitting Yes in alert should save the values.
//When retrieving the SelectedSeries object it shows the recent object selected but i need the last selected one.
Save(m_PrevSelectedSeries);
}
else
{
//Discard function
}
}
//After a successfull save or discard false is returned
return false;
}
private void Save(object mPrevSelectedSeries)
{
//perform the save logic
}
我建议您保存以前的选择,当您需要保存时,为前一个做。
问候