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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:05:02+00:00 2026-06-12T13:05:02+00:00

If have an own user control with a DependencyProperty and the corresponding callback method

  • 0

If have an own user control with a DependencyProperty and the corresponding callback method like below:

public partial class PieChart : UserControl
{
    public static readonly DependencyProperty RatesProperty = DependencyProperty.Register("Rates", typeof(ObservableCollection<double>), typeof(PieChart),
        new PropertyMetadata(new ObservableCollection<double>() { 1.0, 1.0 }, new PropertyChangedCallback(OnRatesChanged)));

[...]

    public static void OnRatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((PieChart)d).drawChart();
    }

When using this user control, I bind an ObservableCollection called “Rates” to the RatesProperty. Rates and the related methods look like this:

private ObservableCollection<double> rates;
public ObservableCollection<double> Rates
{
    get { return this.rates; }
    set
    {
        if (this.rates != value)
        {
            this.rates = value;
            OnPropertyChanged("Rates");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

When I change the ObservableCollection Rates (e.g. with this.Rates = new ObservableCollection<double>() { 1.0, 2.0 }) the OnRatesChanged() method of the user-control is called like expected. But when I execute the following, it is not called:

this.Rates[0] = (double)1;
this.Rates[1] = (double)2;
OnPropertyChanged("Rates");

I expected that when I raise the PropertyChanged event with the correct property name, the corresponding callback in the user control is always called. Is that wrong?
I found this thread: Getting PropertyChangedCallback of a dependency property always – Silverlight
which covers silverlight but I think then that the same is true in WPF.

So the Framework in the background checks if the bound property (in my example “Rates”) changed and only if it changed, it calls the associated callback, correct? Thus changing the elements of my collection has no effect, I always have to change the complete collection?

Thank you!

  • 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-12T13:05:04+00:00Added an answer on June 12, 2026 at 1:05 pm

    Your conclusion is right, your OnRatesChanged callback will only be called when the Rates dependency property is set to a new collection (or null).

    In order to get notified about changes in the collection, you would also have to register a NotifyCollectionChangedEventHandler:

    private static void OnRatesChanged(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var pieChart = (PieChart)d;
        var oldRates = e.OldValue as INotifyCollectionChanged;
        var newRates = e.NewValue as INotifyCollectionChanged;
    
        if (oldRates != null)
        {
            oldRates.CollectionChanged -= pieChart.OnRatesCollectionChanged;
        }
    
        if (newRates != null)
        {
            newRates.CollectionChanged += pieChart.OnRatesCollectionChanged;
        }
    
        pieChart.drawChart();
    }
    
    private void OnRatesCollectionChanged(
        object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            ...
        }
    
        drawChart();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control like this: <UserControl x:Class=MyApp.UserControls.MyContextMenu 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
I am trying to make my own user control and have almost finished it,
I would like to make my own user-mode NAT application. I have the folowing
I have own project and i would like add for this class same as
I have this own class : public class PeriodContainerPanel:StackPanel { public PeriodContainerPanel() : base()
I have an ASP.NET Page that contains a User control called ReportCtrl (my own
I have two problems with an own user control which uses bitmaps: It flickers
I have a class inheriting from UserControl, with a custom DependencyProperty. I put a
So the problem is the following : i do have own user control. which
I am used to ASP.NET where each user control would have its own codebehind,

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.