I have a wpf application that displays a ‘Node’ as such:
<TreeView HorizontalAlignment="Stretch" Name="treeView1" VerticalAlignment="Stretch" Width="Auto" Margin="6,6,6,35">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Node}" ItemsSource="{Binding Children}">
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsThreeState="{Binding HasChildren}" IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding Text}" Click="RefreshTree" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
I would like to do the following:
private void RefreshTree(object sender, RoutedEventArgs e)
{
Node leaf = sender as Node;
// Do stuff with leaf
}
This fails to cast, as object is of type Controls.CheckBox. Is there a way to get the sender of the event as my underlying template type (Node)?
Many thanks!
Your Node will be available as the DataContext, like so:
That’s what the Bindings in your XAML are using also.