I need to bind something to a child of an element in my VisualTree .
in a UserControl:
<StackPanel>
<DataGrid x:Name="dataGrid" />
<Control Tag="{Binding ElementName=dataGrid}" />
</StackPanel>
in DataGrid's Template :
<Template TargetType=DataGrid>
......
<Control x:Name="FindMe" />
......
</Template>
What i thought of doing is traversing the VisualTree of the DataGrid , for this purpose iv'e created a custom markup extension :
public class TemplatePartBinding : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
Binding binding = new Binding();
binding.ElementName = ElementName;
// HOW DO I GET THE SOURCE OBJECT FROM THE BINDING ?
DataGrid dataGrid = // Extract the DataGrid from the binding.
Control _findMe = VisualTreeHelperExtentions.FindVisualChild<Control>(dataGrid,"FindMe");
binding.Target = _findMe;
binding.Path = new PropertyPath("Tag");
return binding;
}
[ConstructorArgument("ElementName")]
public string ElementName
{
get;
set;
}
[ConstructorArgument("TemplatePartName")]
public string TemplatePartName
{
get;
set;
}
}
Here in ProvideValue i wan't to locate the DataGrid (Source Object for the binding ) after giving the binding's ElementName value it's name ,
How do i extract the DependencyObject (My DataGrid) from the binding iv'e just created ?