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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:17:25+00:00 2026-06-11T20:17:25+00:00

I have created a user control with collection property: public static readonly DependencyProperty MyListProperty

  • 0

I have created a user control with collection property:

    public static readonly DependencyProperty
    MyListProperty = DependencyProperty.Register(
        "MyList",
        typeof(ObservableCollection<Test>),
        typeof(UserControl1),
        new FrameworkPropertyMetadata(new ObservableCollection<Test>())
        );

    public ObservableCollection<Test> MyList
    {
        get { return (ObservableCollection<Test>)base.GetValue(MyListProperty); }
        set { base.SetValue(MyListProperty, value); }
    }

    public static readonly DependencyProperty
    BProperty = DependencyProperty.Register(
        "B",
        typeof(string),
        typeof(UserControl1),
        new FrameworkPropertyMetadata(null)
    );

    public string B
    {
        get { return (string)base.GetValue(BProperty); }
        set { base.SetValue(BProperty, value); }
    }

The Test class is:

public class Test : DependencyObject
{
    public static readonly DependencyProperty
    AProperty = DependencyProperty.Register(
        "A",
        typeof(string),
        typeof(Test),
        new FrameworkPropertyMetadata(null)
    );

    public string A 
    {
        get { return (string)base.GetValue(AProperty); }
        set { base.SetValue(AProperty, value); }
    }
}

Then, i’m trying to use my control for binding:

    <TextBox x:Name="tb1" Text="def"/>
    <my:UserControl1 x:Name="uc1" B="{Binding ElementName=tb1, Path=Text}">
        <my:UserControl1.MyList>
            <my:Test A="{Binding ElementName=tb1, Path=Text}"></my:Test>
            <my:Test A="100"></my:Test>
        </my:UserControl1.MyList>
    </my:UserControl1>

The first binding (with B property of User Control) works correctly. The problem is with second binding (with A property of Test which is MyList element). When debugging i have two items in MyList but the A property of the first one is null. Please tell me what I am missing here?

  • 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-11T20:17:27+00:00Added an answer on June 11, 2026 at 8:17 pm

    The problem here is, that the Binding to ElementName=tb1 can not be resolved, even it will never be evaluated. A Binding to an ElementName is resolved for DependencyObjects which are in the visual or logical Tree of a WPF Application. Adding items to your ObservableCollection (MyList) only means adding the items to the Collection, but not into the Visual Tree.

    Edit:
    Here is the approach discussed in the comments:

    In your Window/Page:

    <Window.Resources>
        <!-- Declare the ViewModel as Resource -->
        <my:ViewModel x:Key="viewModel">
            <my:ViewModel.MyList>
                <my:Test A="Hello sweet" />
                <my:Test A="ViewModel" />
            </my:ViewModel.MyList>
        </my:ViewModel>
    </Window.Resources>
    
    <!-- Assign Ressource as DataContext -->
    <StackPanel DataContext="{StaticResource viewModel}">
    
        <TextBox x:Name="tb1" Text="def"/>
    
        <!-- Reference your list within the ViewModel -->
        <ListBox ItemsSource="{Binding Path=MyList}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <!-- Bind your property  -->
                    <TextBlock Text="{Binding Path=A}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    

    And the implementation of ViewModel:

    public class ViewModel
    {
        public ViewModel()
        {
            this.MyList = new ObservableCollection<Test>();
        }
    
        public ObservableCollection<Test> MyList { get; set; }
    }
    

    Of course, class Test no longer needs to implement a DependencyObject. Simple get/set Properties are okay.

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

Sidebar

Related Questions

I have created a user control (CheckedDirTree) that exposes a CheckedFolder property which in
I have created a user control (link) which has a label and a button.
I have created an Address user control which is as follow: <%@ Control Language=C#
I have created a COM enabled .NET User Control and am wondering what is
I have created a wpf application into which I have added a user control
I have created a UserControl in WPF. This user control consists of a border,
I have a user control that I've created, however when I go to add
I have created a web user control (MemberDetails.ascx) which is loaded dynamically on a
I have created a user control that has an asp:login control in it. I
I have a user control - say ControlBase. It has SomeItems property, which is

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.