Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6575119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:20:53+00:00 2026-05-25T15:20:53+00:00

Before anything else, I do apologize for the following verbose question. Since I’m new

  • 0

Before anything else, I do apologize for the following verbose question. Since I’m new to WPF, I decided to explain more in order to probably get more tips!


I have a UserControl like:

<UserControl x:Class="MyNamespace.MyUserControl2"...
             xmlns:local="clr-namespace:MyNamespace"
             Style="{DynamicResource ResourceKey=style1}">
    <UserControl.Resources>
        <Style x:Key="style1" TargetType="{x:Type UserControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type UserControl}">
                        ...
                        <local:MyUserControl1 x:Name="myUserControl1" .../>
                        ...
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
</UserControl>

To access to the myUserControl1 from the code behind, I used a property.

private MyUserControl1 _myUserControl1;
private MyUserControl1 myUserControl1
{
    get
    {
        if (_myUserControl1 == null)
            _myUserControl1 = this.Template.FindName("myUserControl1", this) as MyUserControl1;

        return _myUserControl1;
    }
}

(Is this a good approach to access a template member?)

On the other hand, there is a dependency property in MyUserControl2 class (say DP1) that is responsible for modifying one of myUserControl1 dependency properties. (Say SomeProperty)

private static void IsDP1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var instance = d as MyUserControl2;
    if (instance != null)
    {
        instance.myUserControl1.SomeProperty = function(e.NewValue);
    }
}

When I tried to run the above code, I noticed that instance.myUserControl1 is null. So I treated it like so:

if (instance != null && instance.myUserControl1 != null)
{
    instance.myUserControl1.SomeProperty = function(e.NewValue);
}

Although this approach solved the issue, it causes myUserControl1.SomeProperty to remain uninitialized. So, I put the following code snippet at the loaded event to resolve it:

private void MyUserControl2_Loaded(object sender, RoutedEventArgs e)
{
    this.myUserControl1.SomeProperty = function(DP1);
}

After that, I encountered another problem!

When I set some value to DP1 using setter attribute of a style, I received a null reference exception that says myUserControl1 property is still null at the loaded event. How can I work around it? -Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:20:53+00:00Added an answer on May 25, 2026 at 3:20 pm

    I guess you haven’t still a clear idea about WPF.

    You are hitting so many troubles because your approach is much “winforms-like”, than functional. WPF makes your life harder if you insist using it in a imperative manner.

    First off, the template represents a function to instructs the WPF engine on how to create the actual visual tree during the run time. You should use the name as reference within a template ONLY inside the hosting control (i.e. MyUserControl2), and getting the instance reference from within the OnApplyTemplate method. Nowhere else.

    Example:

    private MyUserControl1 _myUserControl1;
    
    public override void OnApplyTemplate()
    {
      this._myUserControl1 = this.GetTemplateChild("myUserControl1") as MyUserControl1;
      //here you should check whether the instance is actually set
    }
    

    The reference of any control is hosted should kept as private: no protected/internal/public exposition of any of the hosted controls.

    Second point: how to bind two properties together.

    Your goal is to “bind” a property of a control with another exposed by the hosted one. This task is absolutely normal, and it is one of the best features offered by WPF.

    Supposing that the two properties share the same type, thus can be bound directly. Within your xaml:

        <ControlTemplate TargetType="{x:Type UserControl}">
             ...
           <local:MyUserControl1 x:Name="myUserControl1" 
       SomeProperty="{Binding Path=DP1, RelativeSource={RelativeSource Mode=TemplatedParent}}"
       .../>
             ...
        </ControlTemplate>
    

    Note that: (1) SomeProperty must be a DependencyProperty, (2) must be writable, (3) DP1 must be also a DP, or -at least- notify any change via the INotifyPropertyChanged pattern.

    The syntax depicted binds normally: SomeProperty = DP1, but not vice versa. If you need an bidirectional mapping, you should add “Mode=TwoWay” inside the “Binding” clause.

    If you want to customize the function that maps the two properties, simply define your own converter via the IValueConverter interface, then declare it in the xaml.

    Here you will find several useful info: http://msdn.microsoft.com/en-us/library/ms752347.aspx

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before anything else, this is the offending bit of code: for (var i in
This question is mostly curiosity than anything else. But I currently place all my
What problems could I get when defining NOMINMAX before anything else in my program?
Is it possible to have Greasemonkey scripts run before anything else on the page?
I am new here, so I apologize if I am doing anything wrong. I
Before anything else, here is the simplified schema ( with dummy records ) of
Error that pops up even before anything else loads: Package Load Failure Package 'Microsoft.TeamFoundation.Client.ServicesHostPackage,
I have sanitize class that I run before anything else on every page of
I want to write a custom validator which doesn't do anything before the user
I need to convert a string to another which has removed anything before the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.