Command fire before object bind WPF

时间:2016-03-02 10:56:31

标签: c# wpf mvvm telerik

I'm working on a WPF application with MVVM pattern using Telerik controls.

Functionality:

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.

Scenario:

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>

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

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.

Expected Result:

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.

1 个答案:

答案 0 :(得分:1)

尝试做下一件事:

已编辑的虚拟机

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

我建议您保存以前的选择以及何时需要保存, 为前一个做。

此致