Maybe title is confusing you, but the problem is concretely here : I have XAML file which there is ControlTemplate for MyType (inherits from ContentControl). And in that Template i set its DataContext like this :
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
And when i want to get DataContext from MyType it shows null for first time. After clicking on it ( it is rectangle in window ) it changes to object what i need. I searched all things, all events what i am doing in LeftButtonDown there is no ApplyTemplate(),UpdateLayout() methods. Even i call these methods it doesn’t help. What need to do ? P.S this DataContext is Parent of this item, i can send is a parameter but there must be other solution
this is part of XAML which Template located:
<Style TargetType="{x:Type s:Connector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:Connector}">
<Grid Name="grid" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Border BorderBrush="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=BorderBrush}" x:Name="border" BorderThickness="2" Background="Transparent">
<Image Source="/DiagramDesigner;component/Resources/1337238611_port.png"/>
</Border>
<Rectangle Fill="Transparent" Margin="-2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And this is where i want to get DataContext:
public DesignerItem ParentDesignerItem //
{
get
{
if (parentDesignerItem == null)
{
parentDesignerItem = this.DataContext as DesignerItem;
// if (parentDesignerItem==null) parentDesignerItem = (this.Template.FindName("grid", this) as Grid).DataContext as DesignerItem;
}
return parentDesignerItem;
}
set
{
parentDesignerItem = value;
}
}
I must say that this Style is in XAML which Style of the DesignerItem also.
The problem is in my algorithm P.S i restore some information from
SQL, and add 2 times moreConnectorswith null parameters and when i want to access it gives me these null objects.