views:

358

answers:

3

Hi, Im having requirement of passing reference of control to another custom control . Like I created custom control which contains dependency property associateDatagridProperty

public static readonly DependencyProperty AssociatedDataGridProperty = DependencyProperty.Register( "AssociatedDatagrid", typeof(DataGrid), typeof(CustomControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault) );

    public Datagrid AssociatedDatagrid
    {
        get { return (Datagrid )base.GetValue(AssociatedDataGridProperty); }
        set { base.SetValue(AssociatedDataGridProperty, value); }
    }

In XAML Im assign Value like this

Here Datagrid is Microsoft WPF toolkit datagrid

<CustomControl x:Name="DatagridPaging"  
               Canvas.Left="24"    
               Canvas.Top="236"
               AssociatedDatagrid="{Binding ElementName=clientsGrid ,Path=Name}">

when i try to access the value of AssociatedDatagrid property it always show null

can any1 tell me is it right way of doing it ?.

With Regards, Mahender

+1  A: 

You don't need Path=Name in your Binding. What you end up doing here instead is passing the value of the Name property of the DataGrid.

Pavel Minaev
Instead of posting another answer (since I agree), I'll just add a note here, in case there's still confusion. Mainly, behind the scenes, the Binding is trying to do the following:AssociatedDatagrid = (clientsGrid.Name as Datagrid);which will come out to null.
Mark Synowiec
I agree with this, +1.
Jeff Wilcox
A: 

could you plz post code for passing reference of an element to another control

A: 

Hello,

Here is the code :

First element which will be referenced in the second one :

<Label x:Name="aGivenNameLabel" Content="kikou lol"/>

The second element :

<ContentControl Content={Binding ElementName=aGivenNameLabel}" />

Good luck !

Jmix90