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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:02:09+00:00 2026-06-01T04:02:09+00:00

So I’m trying to make a custom user control for a Windows Phone 7

  • 0

So I’m trying to make a custom user control for a Windows Phone 7 application that I call a ColoredTextBlock. You can probably guess what it does.

Anyway, ColoredTextBlock contains a TextBlock which I want the user to be able to set the Text and Style for.

If I try just making a simple property that just passes through, such as:

    public string Text
    {
        get { return Label.Text; }
        set
        {
            Label.Text = value;
            NotifyPropertyChanged("Text");
        }
    }

It causes a very cryptic ArgumentException. However if I set the input text, like:

 <MyRepresentative:ColoredTextBlock Text="Some Text" BackgroundColor="Red" />

Everything works out exactly as I’d expect.

If on the other hand, I go with the more advanced route of using a Dependency property and binding the Inner TextBlock to this property, and then binding the external data to this property as well, nothing shows up.

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set
        {
            SetValue(TextProperty, value);
            NotifyPropertyChanged("Label");
        }
    }

    public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(ColoredTextBlock), null);

Again though, if I insert the text manually, the whole thing just works.

Here’s the xaml of my custom control:

 <UserControl
    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"
x:Class="MyRepresentative.ColoredTextBlock"
d:DesignWidth="456" d:DesignHeight="43"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Rectangle Stroke="Black">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="{Binding DimBackgroundColor}" Offset="0"/>
                    <GradientStop Color="{Binding BrightBackgroundColor}" Offset="0.85"/>
                    <GradientStop Color="{Binding BrightBackgroundColor}" Offset="0.15"/>
                    <GradientStop Color="{Binding DimBackgroundColor}" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock Text="{Binding Text}" Margin="5,0" d:LayoutOverrides="Width"/>
    </Grid>
 </UserControl>

I’ve been racking my brain on this for the better part of the day, and looking at so many different articles, and I’m certain at this point that I’m missing something small, but I just can’t find it.

UPDATE 1: After looking into it further, it appears that for some reason even though I set it up with bindings, it doesn’t appear that this is actually being set, at least as far as I can tell.

UPDATE 2: Based on the comment, you asked about making sure that my DataContext was set correctly. Ya, this was one of the first things I thought of. I have a lines below in my xaml.

 <MyRepresentative:ColoredTextBlock Text="{Binding Title}" BackgroundColor="Red" />
 <TextBlock Text="{Binding Title}" Style="{StaticResource PhoneTextLargeStyle}" />

So the first element doesn’t show up (at all), unless I change to something like Text="Some text". The second element works perfectly without error.

  • 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-01T04:02:11+00:00Added an answer on June 1, 2026 at 4:02 am

    I ended up figuring out the answer to this problem myself, although it took me a while, and a bit of searching. The answer was actually inspired by this answer.

    Answer:

    Instead of binding to the root element using:

     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    

    Such as I did in the example above:

     <UserControl
         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"
         x:Class="MyRepresentative.ColoredTextBlock"
         d:DesignWidth="456" d:DesignHeight="43"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
    

    Bind instead to the LayoutRoot, in this case, my Grid Element.

     <Grid x:Name="LayoutRoot">
    

    This can be done in your code behind, in the constructor, like I did here:

        public ColoredTextBlock()
        {
            ...
            LayoutRoot.DataContext = this;
            ...
        }
    

    Explanation:

    I don’t fully understand why this works to be honest, and if someone can explain it, please edit.

    It seems to have to do with overriding existing bindings when you bind to the root element. As such, when you try to bind an external element to an element of this control, it will fail, because that binding isn’t accessible.

    Hope that helps others who come across the same problem as me!

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.