AgainMe AgainMe - 2 months ago 14
C# Question

How to get the DataContext of VisualParent?

I've a

ToggleButton
with this structure:

private void ToggleButton_Checked(object sender, System.Windows.RoutedEventArgs e)
{
//store settings
}


I need to get the details of
VisualParent
, in particular the
DataContext
, how can I do this? Thanks.

UPDATE

The xaml structure is this:

<ToggleButton Focusable="False" Width="19" Height="19" IsChecked="{Binding Path = IsFavourite}"
Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked">

Answer
((sender as System.Windows.Controls.Primitives.ToggleButton).Parent as Grid).DataContext

Instead of grid you can have a different control. So replace that as per your xaml structure.