liranxs
liranxs

Reputation: 67

How to extract the Binding of a DataTemplate through its child elements?

I have a DataTemplate that is bound to a buisness class, it also contains a StackPanel:

    <DataTemplate DataType="{x:Type logic:Sensor}">
        <StackPanel Name="SensorPanel" MouseDown="SensorPanel_MouseDown">
            <TextBlock Name="ID" Text="{Binding}" ></TextBlock>
            <TextBlock Name="Type" Text="{Binding Type}"></TextBlock>
        </StackPanel>
    </DataTemplate>

When I click on the Stack panel I want to get the instance of the 'logic:sensor' via the sender parameter.

    private void SensorPanel_MouseDown(object sender, MouseButtonEventArgs e)
    {

    }

Is there any way to get to it this way?

Upvotes: 2

Views: 117

Answers (1)

flq
flq

Reputation: 22859

Cast the sender to FrameworkElement and retrieve the DataContext property.

Upvotes: 1

Related Questions