I am working on a WPF application with an MVVM pattern using Telerik controls.
Functionality:
I use telerik:RadListBox , which is generated at runtime according to the number of records. So, if I have 10 entries in my collection, then 10 RadListBox will be shown in the application. When I select each RadListBox , the detailed view (Related values) of the SelectedItem will be shown in the nearest panel. Only one RadListBox can be selected at a RadListBox .
Scenario:
So, after choosing RadListBox and editing the related information in the panel, and when I switch to another RadListBox , a warning (Yes / No) will RadListBox “Do you want to save the details?”. I have implemented INPC in every object and I will check if it has been modified.
// 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>
// Key object:
SelectedItem="{Binding SelectedSeries, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
// ViewModel:
/// <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; }
Now the problem is that after editing the series and when switching the warning (Yes / No) is issued. Then I click save, but the second object is selected and attached to the SelectedSeries object. When I try to save the last edited object, I could not get these values.
I need a little Command to run the SelectedSeries get bind object. So that I can check if the property has changed, and I need to restrict the object getting the binding to the second selected value. Once the save is done for the previous value, then SelectedSeries should be bound to the object.
Expected Result:
After the first selected series is edited and switched to the next, a warning must be raised and, as a result of the warning, the series must be saved or discarded, and it must go to the next selected series.