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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:22:33+00:00 2026-06-15T02:22:33+00:00

I have a custom control which is having a dependency property defined in it

  • 0

I have a custom control which is having a dependency property defined in it and my control implements INotifyPropertyChanged interface.

Dependency Property is Collection of Custom Objects.

Scenario 1

DP is of type List, whatever change I make in the list, nothing updated in MainUI, because I believe WPF does not understand adding and removing objects in list. it understands completely new references so to achieve this, whenever I want to update my list on control I use

MyProperty=new List();

In this approach, my DP callback fires everytime but eventArgs.NewValue always remains zero(it updated the list on UI correctly) but because I need to write some logic in property changed callback based on e.NewItems.Count, in this case that didn’t work. Please tell me why e.NewItems does not work.

Scenario 2

DP is of type ObservableCollection, so as my collection property in view model against which I am binding my DP. in this case my property change callbacks does not fire at all, because I never use “new” keyword again after initialzing the property first time. UI updates but property change still not fires. So my logic in property change call back does not gets executed.

How should I make any of them or both of them working.

  • 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-15T02:22:34+00:00Added an answer on June 15, 2026 at 2:22 am

    I would use the ObservableCollection approach, and subscribe to it’s CollectionChanged event.
    That way you will get notified whenever the collection has been changed.

    But the other approach should work as well. When you set the regular list to a new instance, the PropertyChangedCallback will be fired for the dependency property, and by examining the DependencyPropertyChangedEventArgs object you can get the new value.

    XAML:

    <StackPanel>
        <Button Content="Add to observablecollection" Click="click1" />
        <Button Content="Set list to new instance" Click="click2" />
    </StackPanel>
    

    Code-behind:

    public partial class Window1 : Window
    {
        public ObservableCollection<string> Strings { get; set; }
    
        public List<string> StringsList
        {
            get { return (List<string>)GetValue(StringsListProperty); }
            set { SetValue(StringsListProperty, value); }
        }
    
        public static readonly DependencyProperty StringsListProperty =
            DependencyProperty.Register("StringsList", typeof(List<string>), typeof(Window), new PropertyMetadata(null, StringsListPropertyChanged));
    
        public Window1()
        {
            InitializeComponent();
    
            Strings = new ObservableCollection<string>();
            Strings.CollectionChanged += strings_CollectionChanged;
    
            StringsList = new List<string> { "Test1", "Test2", "Test3", "Test4" };
        }
    
        void strings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //Fires everytime the observablecollection has an item added/removed etc.
            MessageBox.Show(string.Format("ObservableCollection has changed! Count is now {0}", this.Strings.Count.ToString()));
    
            if (this.Strings.Count == 10)
                Console.WriteLine("Collection contains 10 strings!!");
        }
    
        private static void StringsListPropertyChanged(DependencyObject e, DependencyPropertyChangedEventArgs args)
        {
            var newCount = ((List<string>)args.NewValue).Count.ToString();
            MessageBox.Show(string.Format("Dependency property has changed! Count is now {0}", newCount));
        }
    
        private void click1(object sender, RoutedEventArgs e)
        {
            this.Strings.Add("Test1");
        }
    
        private void click2(object sender, RoutedEventArgs e)
        {
            this.StringsList = new List<string> { "Newitem1", "Newitem2" };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a custom control that 'can' have a ListCollectionView passed as ItemsSource, which
I have a custom object type dependency property that is being populated at runtime.
I have a custom control which displays results of some operations. It is hidden
I have a custom server control which wraps a RadEditor (basically a textarea). I
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have a DropDownList as a custom control inserted into a subform which is
I have a custom control with a public collection marked as DesignerSerializationVisibility.Content . When
I have a somewhat complex custom user control, created in C# .NET, which is
I have created a custom user control, which is basicly a ItemsControl with it's
I have a custom server control (composite control having dynamically created dropdown boxes 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.