0

I have a uCtrl of type UserControl in a parentPg of type Page (instead of a Window). And I want to access that parentPg in uCtrl class so that I can bind some uCtrl's property to parentPg's Control.

Like as we get in case of Window.

public partial class uCtrl : UserControl
{
    Window parentWin;

    public uCtrl()
    {
        parentWin = Window.GetWindow(this);
    }

    public bool IsPrptReady
    {
        get
        {
            //accesing parent Window's control

            if (((pWin)(parentWin)).txt.Text != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }   
}

In above code snippet, I am getting parent Window (i.e. of type pWin) in uCtrl of type UserControl and then sets its Property (i.e IsPrptReady) based on parents Window's Control i.e. txt of type 'TextBox'.

I want to do the same thing, but in case of Page not Window. I have seen a lot of questions like this but nothing solves my problem. Any help will be appreciated.

The XAML code is as below.

<Page x:Class="Stego.Pages.EnDeCoding"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Test"
      xmlns:userControls="clr-namespace:Test.UserControls"
      mc:Ignorable="d" 
      d:DesignHeight="600" d:DesignWidth="800"
      >
      <Grid>
           <userControls:uCtrl x:Name="uCtrlName"/>
      </Grid>
</Page>
Community
  • 1
  • 1
Waqas Shabbir
  • 755
  • 1
  • 14
  • 34

1 Answers1

1

The second answer in the question you linked to provides the answer you need; specifically the use of the VisualTreeHelper.

Alternatively, you could add a [dependency] property to the user control of type Page and bind the property to the page instance the control is hosted within. Something like:

<Page x:Name="page">
  <local:UCtrl Page="{Binding ElementName=page}"/>
</Page>
Community
  • 1
  • 1
ibebbs
  • 1,963
  • 2
  • 13
  • 20
  • i have seen that answer and not found my solution. @ibebbs Can you please share a piece of code C# as per my requirement? – Waqas Shabbir Oct 23 '16 at 04:46