在对象绑定 WPF 之前触发命令 [英] Command fire before object bind WPF
问题描述
我正在使用 Telerik 控件开发具有 MVVM
模式的 WPF
应用程序.
I'm working on a WPF
application with MVVM
pattern using Telerik controls.
功能:
我正在使用 telerik:RadListBox
,它是在运行时根据记录数生成的.因此,如果我的集合中有 10 条记录,则应用程序中将显示 10 个 RadListBox
.当我选择每个 RadListBox
时,SelectedItem
的详细视图(相关值)将显示在附近的面板中.一次只能选择一个RadListBox
.
I'm using telerik:RadListBox
which is generated in run time according to number of records. So, if i have 10 records in my collection 10 RadListBox
will be shown in the application. When i select each RadListBox
a detailed view(Related Values) of the SelectedItem
will be shown in the nearby panel. Only one RadListBox
can be selected at a time.
场景:
所以在选择一个 RadListBox
并在面板中编辑相关信息后,当我切换到另一个 RadListBox
时,将抛出警报(是/否)你要保存详细信息吗?".我已经在每个对象中实现了 INPC
,我会检查它是否被更改.
So after selecting a RadListBox
and editing the related information in the panel and when i switch to another RadListBox
an alert(Yes/No) will be thrown "Do you want to save the details?". I have implemented the INPC
in each object and i'll check if it is changed.
//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
对象中.当我尝试保存最后编辑的对象时,我无法获得这些值.
Now the issue is, after editing a series and when switching the alert(Yes/No) is thrown. Then i'm hitting save, but the second object is selected and got binded in the SelectedSeries
object. When i try to save the last object edited i couldn't able to get those values.
我需要一些 Command
在 SelectedSeries
对象绑定之前触发.这样我就可以检查属性是否已更改,并且我必须将对象绑定到所选的第二个值.为前一个值完成保存后,必须将 SelectedSeries
绑定到对象.
I need some Command
to fire before the SelectedSeries
object get bind. So that i can check if the property is changed and i have to restrict the object getting bind to the second value selected. Once the save is done for the previous value then the SelectedSeries
has to be bind to the object.
预期结果:
一旦第一个选定的系列被编辑并切换到下一个,应该抛出警报,作为警报的结果,该系列应该被保存或丢弃,它应该移动到下一个选定的系列.
Once the first selected series is edited and switched to next, the alert should be thrown and in result of the alert the series should get saved or discard and it should move to next series which is selected.
推荐答案
尝试做下一件事情:
编辑的虚拟机
/// <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
}
我建议你保存之前的选择,当你需要保存时,为上一个做.
I suggesting you to save the previous selection and when you need to save, do it for the previous one.
问候,
这篇关于在对象绑定 WPF 之前触发命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!