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

  • Home
  • SEARCH
  • 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 6773935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:43:37+00:00 2026-05-26T15:43:37+00:00

I have the following simple custom control that I have defined for a Windows

  • 0

I have the following simple custom control that I have defined for a Windows Phone Application. The XAML for the control is as follows :

<UserControl x:Class="PhoneApp1.MyControl"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
         d:DesignHeight="480"
         d:DesignWidth="480"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">

<Grid x:Name="LayoutRoot">
    <TextBlock Text="{Binding MyLabel}" />
</Grid>

In addition the user control has a dependency property which looks as follows :

public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MyLabelProperty =
        DependencyProperty.Register("MyLabel", typeof (string), typeof (MyControl), new PropertyMetadata(default(string)));

    public string MyLabel
    {
        get { return (string) GetValue(MyLabelProperty); }
        set { SetValue(MyLabelProperty, value); }
    }
}

Now the thing is when I try to databind it to a View Model that I have created, nothing happens. The various code items are as follows :

public class MyControlViewModel : ViewModelBase
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value;
        RaisePropertyChanged(() => Name);}
    }
}

View Model declare in App.xaml

<Application.Resources>
    <PhoneApp1:MyControlViewModel x:Key="MCVM" />
</Application.Resources>

MainPage.xaml declaration of the control

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <PhoneApp1:MyControl  x:Name="testControl" DataContext="{StaticResource MCVM}" MyLabel="{Binding Path=MyLabel}" />            
    </Grid>

However, if I try to databind it to some other UI Element as follows,it seems to work?

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <PhoneApp1:MyControl  x:Name="testControl" MyLabel="{Binding ElementName=Button, Path=Content}" />
        <Button Click="Button_Click" x:Name="Button" Content="Click" />
    </Grid>

I have been struggling to get this simple scenario to work, and I think that there is something silly that I’m missing?

Any help would be greatly appreciated

  • 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-26T15:43:38+00:00Added an answer on May 26, 2026 at 3:43 pm

    (This is WPF; I’m not sure if this isn’t 100% analogous to WP7 silverlight; I provide further details to help you understand what’s going on so that you may research a proper solution if my two options aren’t available)

    By default, bindings are rooted at the DataContext of the root element of the xaml file (be it a Window or a UserControl). That means that

    <TextBlock Text="{Binding MyLabel}" />
    

    attempts to bind to the DataContext. So, essentially, the property you are attempting to bind against is

    MyControl.DataContext.MyLabel

    You need to bind to the following instead:

    MyControl.MyLabel

    So, you must rebase the binding to the element in the visual tree you wish to bind against, the MyControl class. You can do this a few ways…

    <TextBlock Text="{Binding MyLabel, 
                     RelativeSource={RelativeSource FindAncestor,
                                                    AncestorType=UserControl}" />
    

    is common, but that has to walk the tree and can cause some performance issues. I find it easier, and recommend, to do the following:

    <UserControl x:Class="PhoneApp1.MyControl"
                 x:Name="root"
                 x:SnipAttributesBecauseThisIsAnExample="true">
        <TextBlock Text="{Binding MyLabel, ElementName=root}" />
    </UserControl>
    

    Give your UserControl a Name, then use ElementName to rebase your binding to the root of the visual tree.

    I’ll leave out the other, wackier ways to accomplish this.

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

Sidebar

Related Questions

I have the following methods in my simple Custom Parser Class, when I execute
I have the following XAML (simple list box with custom DataTemplate). I'm trying to
I have a simple custom user control that uses jqGrid. the control is as
I have the following XAML code as part of a custom control: <telerik:RadTreeView x:Name=treeModules>
I have the following lines in my ~/.emacs.d/init.el (custom-set-variables '(flymake-allowed-file-name-masks (quote ( (\\.cc\\' flymake-simple-make-init)
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
We have the following simple Stored Procedure that runs as an overnight SQL server
I have the following simple python test script that uses Suds to call a
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I created a simple custom control that only inherits from the Literal control, and

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.