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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:12:04+00:00 2026-06-06T22:12:04+00:00

This is probably not a rocket science question, so forgive me for being a

  • 0

This is probably not a rocket science question, so forgive me for being a newcomer!
I have a UserControl that is for setting the name of a person (simple for test purposes).

PersonNameControl.xaml:

<UserControl x:Class="BindingPropagationTest.Controls.PersonNameControl"
             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" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Width="120" Height="23" Margin="0,0,0,0"
             >
    <TextBox Name="TextBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</UserControl>

and as you can see, it contains a TextBox that is the “real” textbox. The code behind looks like this.

PersonNameControl.xaml.cs:

using System.Windows.Controls;
using System.Windows;
using System.Diagnostics;

namespace BindingPropagationTest.Controls
{
    public partial class PersonNameControl : UserControl
    {
        public static DependencyProperty nameProperty
            = DependencyProperty.Register("PersonName", typeof(string), typeof(PersonNameControl));

        public string PersonName
        {
            get
            {
                Debug.WriteLine("get PersonNameControl.PersonName = " + TextBox.Text);
                return TextBox.Text;
            }

            set
            {
                Debug.WriteLine("set PersonNameControl.PersonName = " + value);
                TextBox.Text = value;
            }
        }

        public PersonNameControl()
        {
            InitializeComponent();
        }
    }
}

I use the usercontrol in MainWindow.xaml:

<Window x:Class="BindingPropagationTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:BindingPropagationTest.Controls"
        xmlns:items="clr-namespace:BindingPropagationTest.ComboBoxItems"
        Title="Testing binding in UserControl"
        Width="179" Height="310">
  <Canvas Height="241" Width="144">

        <Label Canvas.Left="11" Canvas.Top="10" Content="Name" Height="28" Padding="0" />
        <my:PersonNameControl x:Name="nameControl"
                Width="120" Height="23"
                HorizontalAlignment="Left" VerticalAlignment="Top"
                PersonName="{Binding name}"
                Canvas.Left="11" Canvas.Top="28" />

        <Label Canvas.Left="11" Canvas.Top="57" Content="Address" Height="28" Padding="0" />
        <TextBox Canvas.Left="11" Canvas.Top="75" Width="120" Text="{Binding address}"></TextBox>

        <Label Canvas.Left="11" Canvas.Top="103" Content="Age" Height="28" Padding="0" />
        <TextBox Canvas.Left="11" Canvas.Top="122" Height="23" Name="textBox1" Width="120" Text="{Binding age}" />

        <ComboBox Canvas.Left="11" Canvas.Top="173" Height="23"
                Name="comboBox1" Width="120" SelectionChanged="comboBox1_SelectionChanged">
            <items:PersonComboBoxItem age="41" name="Donald Knuth" address="18 Donut Street" Height="23" />
            <items:PersonComboBoxItem age="23" name="Vladimir Putin" address="15 Foo Street" Height="23" />
            <items:PersonComboBoxItem age="32" name="Mike Hammer" address="10 Downing Street" Height="23" />
        </ComboBox>
    </Canvas>
</Window>

together with some normal TextBoxes as you can see.

In the code behind for MainWindow

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using BindingPropagationTest.ComboBoxItems;

namespace BindingPropagationTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new Person();
        }

        private void comboBox1_SelectionChanged
            (object sender, SelectionChangedEventArgs e)
        {
            updateForm();
        }

        private void updateForm()
        {
            var item = (PersonComboBoxItem)comboBox1.SelectedItem;

            DataContext = new Person()
            { age = item.age, name = item.name, address = item.address };           
        }
    }
}

you see that I set DataContext to a “person”.

Person.cs:

namespace BindingPropagationTest
{
    public class Person
    {
        public string name {get; set; }
        public int age { get; set; }
        public string address { get; set; }
    }
}

and as you notice I have invented an own ComboBoxItem like this.
PersonComboBoxItem.cs:

using System.Windows.Controls;
using System.Diagnostics;

namespace BindingPropagationTest.ComboBoxItems
{
    public class PersonComboBoxItem : ComboBoxItem
    {
        private string _name = "";
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                Content = _name;
            }
        }

        public int age { get; set; }
        public string address { get; set; }

        public override string ToString()
        {
            Debug.WriteLine("name: " + name);
            return name;
        }
    }
}

Running this application gives you this window:

enter image description here

And selecting a combobox item gives you this:

enter image description here

and as you can see, the name will not be filled in.
Why not?

  • 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-06T22:12:05+00:00Added an answer on June 6, 2026 at 10:12 pm

    You are almost there, a few things you need to change

    Dependency properties are used like

        public static DependencyProperty NameProperty = DependencyProperty.Register(
        "Name",
        typeof(string),
        typeof(PersonNameControl));
    
        public string Name
        {
            get
            {
                return (string)GetValue(NameProperty);
            }
            set
            {
                SetValue(NameProperty, value);
            }
        }
    

    There is a very strict convention that is necessary for dependency properties. It must be called “NameProperty” if the Property is called Name. Also the property just sets and gets the dependency property.

    You can them Bind the textbox to the property in the usercontrol like

    <UserControl x:Class="BindingPropagationTest.Controls.PersonNameControl"
             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" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             x:Name="UserControl"
             mc:Ignorable="d" Width="120" Height="23" Margin="0,0,0,0"
             >
        <TextBox Text="{Binding ElementName=UserControl, Path=Name}" Name="TextBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </UserControl>
    

    I added the x:Name=”UserControl” directive at the top, you could name it anything, as long as it matches the ElementName in the Binding

    An extra note on how you are populating your comboBox

    You could add a property on your MainWindow

        private ObservableCollection<Person> _thePeople;
        public ObservableCollection<Person> ThePeople
        {
            get
            {
                if (_thePeople == null)
                {
                    List<Person> list = new List<Person>()
                    {
                        new Person() { name = "Bob" , address = "101 Main St." , age = 1000 },
                        new Person() { name = "Jim" , address = "101 Main St." , age = 1000 },
                        new Person() { name = "Mip" , address = "101 Main St." , age = 1000 },
                    };
                    _thePeople = new ObservableCollection<Person>(list);
                }
                return _thePeople;
            }
        }
    

    then you can add to your main window the x:Name directive used on the usercontrol say

    x:Name="TheMainWindow"
    

    You can then use a datatemplate in your combobox as follows

        <ComboBox ItemsSource="{Binding ElementName=TheMainWindow, Path=ThePeople}"
              Height="23"
              Name="comboBox1" Width="120" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding name}" />
                        <TextBlock Text="{Binding address}" />
                        <TextBlock Text="{Binding age}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    

    Because ObservableCollection was used in the code-behind the combobox will automatically get items added or removed whenever the code-behind adds or removes items from the “ThePeople” collection. Just call

    ThePeople.Add(new Person());
    

    and the combobox will automatically populate with a new entry

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

Sidebar

Related Questions

i'm very aware that this is probably not a stackoverflow question, but i figure
I have a feeling that this probably is not possible using strictly CSS, but,
This is probably not a dup; I have read through many similar problems on
Okay, I know that 1) this is probably not possible with CSS alone and
I'm used to MSSQL, not Mysql, so sorry for this probably stupid question. I'm
I know this is probably not possible but let's say I have a model
This is probably not the wisest question, but is it possible to return a
This is probably not a very difficult question to answer. I'm having trouble with
I suspect this is probably not possible due to browser security, however I have
This is probably not a simple question so I am not looking for a

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.