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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:20:12+00:00 2026-05-24T13:20:12+00:00

I thought this would be pretty simple to do but seems I must be

  • 0

I thought this would be pretty simple to do but seems I must be missing something blinding obvious.

The problem is that I am passing values to my UserControl (BoxPanel) but the values are not displayed. The blue box is displayed without text.

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <l:BoxPanel Number="1" Text="Hi" />
    </Grid>
</Window>

BoxPanel.xaml

<UserControl x:Class="WpfApplication1.BoxPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         Height="50" Width="90">
<Border Background="Blue">
    <StackPanel>
        <TextBlock FontSize="20" HorizontalAlignment="Center" 
            Text="{Binding Number}" />
        <Label FontSize="10" HorizontalAlignment="Center" Foreground="White" 
            Content="{Binding Text}" />
    </StackPanel>
</Border>

BoxPanel.xaml.xs

public partial class BoxPanel : UserControl
{
    public static readonly DependencyProperty NumberProperty = 
        DependencyProperty.Register("Number", typeof(decimal), typeof(BoxPanel));
    public static readonly DependencyProperty TextProperty = 
        DependencyProperty.Register("Text", typeof(string), typeof(BoxPanel));

    public BoxPanel()
    {
        InitializeComponent();
    }

    public decimal Number
    {
        get { return (decimal)GetValue(NumberProperty); }
        set { SetValue(NumberProperty, value); }
    }

    public string Text
    {
        get { return (string)base.GetValue(TextProperty); }
        set { base.SetValue(TextProperty, 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-24T13:20:14+00:00Added an answer on May 24, 2026 at 1:20 pm

    Binding paths, by default, are rooted at the DataContext. But you wish to bind to properties defined on the UserControl. So you have to redirect them somehow. I usually just do it by ElementName.

    <UserControl x:Class="WpfApplication1.BoxPanel"
             x:Name="BoxPanelRoot"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             Height="50" Width="90">
    <Border Background="Blue">
        <StackPanel>
            <TextBlock Text="{Binding Number, ElementName=BoxPanelRoot}" />
            <Label Content="{Binding Text, ElementName=BoxPanelRoot}" />
        </StackPanel>
    </Border>
    

    It seems a little odd at first, and somewhat annoying to redirect bindings like this, but it is preferrable than other methods which utilize the DataContext within the UserControl. If you block the DataContext by, say, setting it to the root of the UserControl, you have effectively blocked the best method of passing data into the UserControl.

    Rule of thumb, when binding in a UserControl, leave the DataContext alone unless you are explicitly binding against data passed to the UserControl.

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

Sidebar

Related Questions

I thought this would be pretty easy but I'm running into all sorts of
Okay at first I thought this would be pretty straightforward. But I can't think
I thought this would be simple, but its not working .. in both the
so i want to do something which seems pretty simple but is proving other
I thought this would be fairly easy, but I'm totally baffled. I want one
I would have thought this would be an easy one to Google, but I've
Before I throw something ugly together I thought this would be a good one
I thought this code would work, but the regular expression doesn't ever match the
I'm trying to write (what I thought would be) a simple bash script that
I thought this would be a pretty common question to look for in Google

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.