Why I am right!

April 25, 2008

SelectionChanged event fires during databinding (Solution)

Filed under: Microsoft, WPF, XAML — Shaun @ 2:54 am

I was having an issue where the SelectionChanged event would fire during WPF DataBinding. While this may be valid, this was not the desired behavior for my application.

Since I could not find anything after a quick Google search I decided to post my workaround. Part of this example was taken from Adam Nathan’s excellent WPF book. Here is some sample code:

Window1.xaml

<Window x:Class="WpfApplication2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="200" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="dataProvider" XPath="GameStats">
            <x:XData>
                <GameStats xmlns="">
                    <GameStat Type="Beginner">
                        <HighScore>1203</HighScore>
                    </GameStat>
                    <GameStat Type="Intermediate">
                        <HighScore>1089</HighScore>
                    </GameStat>
                    <GameStat Type="Advanced">
                        <HighScore>541</HighScore>
                    </GameStat>
                </GameStats>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource dataProvider}, XPath=GameStat/HighScore}"
                SelectedValue="1203" SelectionChanged="ListBox_SelectionChanged" />
    </Grid>
</Window>

Window1.xaml.cs

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication2
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      ListBox listBox = (ListBox)sender;
      if (listBox.IsLoaded)
      {
        Debug.WriteLine("ListBox Selection REALLY Changed.");
      }
      else
      {
        Debug.WriteLine("ListBox Selection Changed but we don't care!");
      }
    }
  }
}

As you can see I am looking at the IsLoaded property which comes from FrameworkElement. This is not set to true until the data binding is complete. I am not sure if this will work 100% of the time, but it works for my needs.

Advertisements

5 Comments »

  1. another good one is to test using
    (!listBox.IsMouseCaptured), so if the user’s mouse is not involved with the control, then the SelectionChanged function does not fire.

    Comment by Josh — September 10, 2008 @ 10:49 pm

  2. Great man….u saved my day…! Thx

    Comment by biju — June 18, 2009 @ 4:22 pm

  3. Thx for the info 🙂 It was really usefull!

    Comment by Frederic — November 27, 2009 @ 4:50 pm

  4. @Josh – What if the listbox is being controlled with the keyboard?

    Comment by xr280xr — July 9, 2011 @ 1:58 am

  5. Thanks Shaun, seems to also works correct for an already rendered / bound combo box control whos binding updates its value (Would appear is IsLoaded is more like IsReady).

    Comment by KVK Consultancy — December 4, 2012 @ 6:09 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: