in the following structure
<Border ...>
<ItemsControl>
<ItemsControl.Template>
<DataTemplate>
<ACustomElement>
<Border MouseLeftButtonDown="method1">
</ACustomElement>
</DataTemplate>
</ItemsControl.Template>
</ItemsControl>
</Border>
I want to call a public method in the ACustomElement class from inside method1().
What I tried so far in method1():
var cr = ((Border)sender).Parent;
cr.method2();
method2 is a public method in my ACustomElement class. But it doesn't seem to recognize the method.
I'm getting the following error:
'DependencyObject' does not contain a definition for 'method2' and no extension method 'method2' accepting a first argument of type 'DependencyObject' could be found (are you missing a using directive or an assembly reference?)
Any suggestions on how to solve this problem? Certainly I'm just missing a cast or something else...
Edit: The following style will always be applied to ACustomElement:
<Style TargetType="{x:Type c:ACustomElement}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:ACustomElement}">
<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You have to cast
crto ACustomElement typeotherwise, your
crvariable refers to DependencyObject type as you see in the exception.if you are not sure about hierarchy use this method to find a parent of specific type.
Also, DateTemplate can be child of ItemsControl.ItemTemplate, but not of ItemsControl.Template (which expects ControlTemplate)
Update
And as I pointed in the comment to the question, add an event handler to your type directly or bind a command. Why do you make it so complicated?