I defined the following DataTemplate for a LibraryContainer:
<DataTemplate x:Key="ContainerItemTemplate">
<Grid>
<Border BorderThickness="1" BorderBrush="White" Margin="3">
<s:SurfaceTextBox IsReadOnly="True" Width="120" Text="{Binding Path=name}" Padding="3"/>
</Border>
<s:SurfaceButton Content="Expand" Click="SourceFilePressed"></s:SurfaceButton>
</Grid>
</DataTemplate>
The SourceFilePressed is the following:
private void SourceFilePressed(object sender, RoutedEventArgs e)
{
Logging.Logger.getInstance().log(sender.ToString());
e.Handled = true;
}
In the method SourceFilePressed who can I get the object that is binded to the SurfaceTextBox that is in the same grid as the pressed button? Can I somehow in the DataTemplate attach this object to the Click-Event?
If I parsed your question correctly, I think you could do this:
To explain: the sender is the source of the event, so it’s the
SurfaceButton. It is aFrameworkElementand thus has aDataContextproperty. TheDataContextis an inherited property, so unless you set it explicitly on theSurfaceButton, it will inherit it’sDataContextfrom its parent (theGrid). TheDataTemplate‘sDataContextis the data item it is templating, so you can see that theSurfaceButtonwill have that same object as itsDataContext.