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 1025749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:58:30+00:00 2026-05-16T11:58:30+00:00

I have a UserControl with a Template property that I’ve set up as a

  • 0

I have a UserControl with a Template property that I’ve set up as a DependencyProperty:

public partial class TemplateDetail : UserControl
{
    public static readonly DependencyProperty _templateProperty =
        DependencyProperty.Register(
            "Template",
            typeof(Template),
            typeof(TemplateDetail)
        );

    public TemplateDetail()
    {
        InitializeComponent();
        Template = new Template();
        DataContext = Template;
    }

    public Template Template
    {
        get
        {
            return (Template)GetValue(_templateProperty);
        }
        set { SetValue(_templateProperty, value); }
    }
}

I’m trying to use this UserControl in the XAML for a Window, SaveTemplateDialog, and I’m trying to set its Template property to the Template property in my SaveTemplateDialog class:

<local:TemplateDetail HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                      MinWidth="100" Template="{Binding}"
                      Width="{Binding ElementName=Scroller, Path=ViewportWidth}"/>

The DataContext in SaveTemplateDialog is set to its Template property, which is also a dependency property. However, for the XAML above, Template="{Binding}" is underlined in blue in Visual Studio and it says:

A ‘Binding’ cannot be set on the ‘Template’ property of type ‘TemplateDetail’. A ‘Binding’ can only be set on a DependencyProperty of a DependencyObject.

Sure enough, when I load my app, the contents of the Template that TemplateDetail tries to display is blank, when I know there are values in it. Why does it give this message if I’ve registered TemplateDetail.Template as a dependency property?

Edit: now I’m quite confused. In my SaveTemplateDialog window, I’m doing the following to pass along the template to the TemplateDetail UserControl:

<local:TemplateDetail HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                      TemplateData="{Binding Path=NewTemplate, Mode=OneWay}"
                      Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"/>

I know Path=NewTemplate is getting the right data because I also have this XAML in SaveTemplateDialog, which shows the value I would expect in the MyType property of NewTemplate:

<TextBlock Text="{Binding Path=NewTemplate.MyType}" />

That TextBlock shows what I expect. However, apparently the right data is not getting to TemplateDetail or I’m not binding correctly there, because I can’t get that same MyType property (or the other Template properties) to show up in TemplateDetail. In TemplateDetail:

<TextBlock Text="{Binding Path=TemplateData.MyType}" Grid.Row="2" Grid.Column="2"
           HorizontalAlignment="Stretch" Width="200"/>

And here’s the TemplateDetail class as it is now:

public partial class TemplateDetail : MainWindowUserControl
{
    public static readonly DependencyProperty TemplateDataProperty =
        DependencyProperty.Register(
            "TemplateData",
            typeof(Template),
            typeof(TemplateDetail),
            new PropertyMetadata(
                new Template("Default Template", null)
            )
        );

    public TemplateDetail()
    {
        InitializeComponent();
        DataContext = this;
    }

    public Template TemplateData
    {
        get
        {
            return (Template)GetValue(TemplateDataProperty);
        }
        set { SetValue(TemplateDataProperty, value); }
    }
}
  • 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-16T11:58:30+00:00Added an answer on May 16, 2026 at 11:58 am

    You should change the name of your DependencyProperty to the use the standard convention like PropertyNameProperty instead of naming like a field. When you set something in XAML it calls SetValue and expects the property to be named using the string name (“Template” here) followed by “Property”.

    UPDATE for second question:

    When you set DataContext = this in your TemplateDetail constructor that is causing the source for your Path=NewTemplate Binding to be changed from the inherited DataContext to the TemplateDetail control itself. You should be seeing an error in your Output window that the NewTemplate property can’t be found on the object of type TemplateDetail. Instead of resetting the DataContext of the UserControl itself, I will usually give the root layout Panel in my UserControl XAML an x:Name and set LayoutRoot.DataContext = this in the constructor instead.

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

Sidebar

Related Questions

I have a UserControl that I've added a Dependency Property to: public partial class
I have a UserControl in my Asp.net project that has a public property. I
I have a usercontrol that has several public properties. These properties automatically show up
I have a UserControl that consists of three TextBoxes. On a form I can
I have a usercontrol that is meant to take up the entire form. I
If I have a usercontrol (in Silverlight) that I've written, that uses XAML to
I have XAML file like this <UserControl x:Class=UISkripsi3.Sidebar xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:UISkripsi3=clr-namespace:UISkripsi3 xmlns:local=clr-namespace:UISkripsi3>
I have a complex WPF UserControl made of other ContentControl templates which contain sets
I have UserControl which contains a TextBox and a Button control. The Button opens
I have a UserControl with some predefined controls (groupbox,button,datagridview) on it, these controls are

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.