0

I have a WPF Datagrid that is bound to a model.

Inside the model I have a property defined as

   public String status
    {
        get
        {
            return m_status;
        }
        set
        {
            m_status = value;
            OnPropertyChanged("status");           
        }
    }

This property informs the grid of changes via OnPropertyChanged.

I also handle the SelectionChanged event to trigger different activities.

 SelectionChanged="gridSongs_SelectionChanged"


    private void gridSongs_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Console.WriteLine("gridSongs_SelectionChanged " + sender.ToString());
    }

During testing this I have noticed that every time I change the property "status" in code the grid updates automatically (which is what I want) but also fires the SelectionChanged Event as well.

Is there any way I can stop the event from firing when I change the model from code but let it go through when user clicks an item in the grid ?

Maybe I could use a different event for the manual selection of items in the grid ?

thanks a lot in advance.

Mozzak
  • 201
  • 3
  • 16

1 Answers1

1

Is there any way I can stop the event from firing when I change the model from code but let it go through when user clicks an item in the grid?

No, but there is a simple workaround. Add a private bool isLocal variable and set it to true before you make any changes and back to false afterwards:

isLocal = true;
status = "Some Value";
isLocal = false;

Then, in your SelectionChanged handler, check this variable and only react if it is false:

private void gridSongs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (!isLocal ) Console.WriteLine("gridSongs_SelectionChanged " + sender.ToString());
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    thanks Sheridan. That will work. I actually implemented another event "mouseup" and moved my logic I had in SelectionChanged event. That seems to work as well. I just needed some extra logic to check if a row is selected or not but I like your version as well. Will give that a try. – Mozzak Apr 17 '15 at 14:22