0

I can't quite understand what's going on with the following bit of code.

It's a relatively simple wpf datagrid which displays an ObservableCollection of objects. If I select a row, and press the delete key, a LostFocus event is fired from the DataGridCell. Using Snoop, I can look at the elements that receive this routed (bubbling) event. It starts at DataGridCell, and bubbles up the visual tree until it hits the DataGridRow, but then it stops (This is all evident in Snoop which displays both unhandled and handled events).

My understanding of bubbling events has led me to believe the LostFocus event should bubble all the way up to the parent window.

What gives?

    <Window x:Class="ApplicationName.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
        <Grid x:Name="_grid">
            <DataGrid
                ItemsSource="{Binding Items}"
                AutoGenerateColumns="False"
                HeadersVisibility="Column"
                CanUserDeleteRows="True"
                SelectionUnit="FullRow">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=FirstName}"
                                Header="FirstName"/>
                    <DataGridTextColumn Binding="{Binding Path=LastName}"
                                Header="LastName"/>
                    <DataGridTextColumn Binding="{Binding Path=Information}"
                                Header="Information"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>

The code-behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }

    public class ViewModel
    {
        public ObservableCollection<Person> _items = new ObservableCollection<Person>();

        public ViewModel()
        {
            _items.Add(new Person() { FirstName = "Homer", LastName = "Simpson", Information = "Father" });
            _items.Add(new Person() { FirstName = "Bart", LastName = "Simpson", Information = "Son" });
            _items.Add(new Person() { FirstName = "Santa's", LastName = "Little Helper", Information = "Dog" });

        }

        public ObservableCollection<Person> Items
        {
            get { return _items; }
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Information { get; set; }
    }
John Ostin
  • 301
  • 2
  • 12
Andrew
  • 1,482
  • 9
  • 16

1 Answers1

0

If the parent Window's LostFocus event fires, this means that the focus is not in the parent Window any more. Do you expect this behavior? I think this is not your case. The DataGridRow losts focus, the DataGridCells lost focus, but not the parent Window.

Ramin
  • 2,133
  • 14
  • 22
  • I don't expect the parent window to lose focus - just for it to get a chance to handle the DataGridCell's LostFocus event. – Andrew Jan 14 '13 at 17:25