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