I have a user control containing an expander. The content of the explander is a ListBox bound to an object, and a DataTemplate displays it correctly. The problem is this: the user can select a Listbox item, and the SelectionChanged handler changed the DataContext of the ListBox to the selected object.
Like this:
<ListBox
Name="RelativesLB" ItemsSource="{Binding Relatives}",
ItemsTemplate ="{...}",
Selectionchanged="Relatives_OnSelectionChanged" />
And:
Relatives_OnSelectionChanged(object sender, ...EventArgs e)
{
var who = (sender as ListBox).SelectedItem as Person;
if (who == null)
return;
People.DataContext = who;
Here is the problem:
- The
SelectionChanged
event fires. - The
DataContext
is changed, and theListBox
repopulates. - The
SelectionChanged
event fires withSelectedItem = null
. Here, my code does not change theDataContext
; it just returns. - the
SelectionChanged
event fires again withSelectedItem = <whatever is first>
. Here, my code changes theDataContext
again to that item I don't want this bit. Actually, I want to stop after 2. the
Datacontext
is changed to<whatever is first>
...
and so on, until we get an emptyPerson.Relatives
, then we stop.
What I want is the stop after the first DataContext
change. You select a person
from the Relatives
collection, and get the view for that person.
How can I stop the subsequent SelectionChanged
events firing?