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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:16:54+00:00 2026-06-13T12:16:54+00:00

I have a user control which I’d like to be used like so: //

  • 0

I have a user control which I’d like to be used like so:

// MainPage.xaml
<my:MyControl Data="10" />
<!-- or -->
<my:MyControl Data="{Binding SomeData}" />

It’s codebind is this:

 public partial class MyControl : UserControl
 {
    public MyControl() {
       InitializeComponent();
    }
   public const string DataPropertyName = "Data";
   public int Data
        {
            get
            {
                return (int)GetValue(DataProperty);
            }
            set
            {
                SetValue(DataProperty, value);
            }
        }

        public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
            DataPropertyName,
            typeof(int),
            typeof(MyControl),
            new PropertyMetadata(10);
 }

It’s xaml portion is this:

 <UserControl>
    <!-- omitted namespaces etc. -->
    <Grid x:Name="LayoutRoot">
         <Button x:Name="myButton" Content="{Binding Data}">
           <Button.Style>
             <Setter Property="Template">
               <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <TextBlock Text="{TemplateBinding Content}" />
                 </ControlTemplate>
                 </Setter.Value>
            </Button.Style>
         </Button>
    </Grid>
   </UserControl>

The crucial line, in usercontrol’s xaml part is:

<Button x:Name="myButton" Content="{Binding Data}"> 

I’d like to bind this Button’s Content property to the UserControl’s property (Data), while still retaining the ability to set values on it from outside (<my:MyControl Data="10" />)

The problem is, that when I use binding – <Button x:Name="myButton" Content="{Binding Data}"> – it doesn’t work (The templatebinding doesnt pick any values)
It works however, if I set the values manually i.e – <Button x:Name="myButton" Content="12">

  • 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-06-13T12:16:55+00:00Added an answer on June 13, 2026 at 12:16 pm

    If you want to bind to your “own” dependency property inside a UserControl you need to add a x:Name to your UserControl and use it as the ElementName in your binding.

    <UserControl x:Name="myControl">
        <!-- omitted namespaces etc. -->
        <Grid x:Name="LayoutRoot">
             <Button x:Name="myButton" 
                     Content="{Binding Data, ElementName=myControl}">        
             </Button>
        </Grid>
    </UserControl>
    

    To make the Template also work:

    Instead of the TemplateBinding you need to use the RelativeSource TemplatedParent sysntax, because you need to set the Mode=OneWay (TemplateBinding uses Mode=OneTime for performance reasons by default but in your scenario you need Mode=OneWay)

    <Style TargetType="Button">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <TextBlock Text="{Binding Path=Content, Mode=OneWay, 
                            RelativeSource={RelativeSource TemplatedParent}}" />
                    </ControlTemplate>    
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control which I call like this: <MyNamespace:MyControl runAt=server ID=foo />
I have a user control which used to display data using a number of
I have a user control which is called MyControl. And it is used in
I have a user control which I would like to add a dependency property
I have a User Control which returns a table of data, which in some
I have created Custom User Control which contain TextBox and PasswordBox. it is binding
I have a user control which I use as a listboxItem. On this user
I have a user control which contains a CustomValidator which is used according to
I have a user control which shows list of latest announcements. This user control
I have a user control which has a number of properties. This user control

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.