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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:50:53+00:00 2026-05-18T02:50:53+00:00

Perhaps I don’t understand events fully. I’m building a Windows Phone 7 app in

  • 0

Perhaps I don’t understand events fully.

I’m building a Windows Phone 7 app in Silverlight.

I have a UserControl that wraps a ListBox, called EditableListBox. The ListBox has a data template. The items in the list box are wrapped by EditableListItem objects.

The data template is as follows:

<DataTemplate>
    <Grid ManipulationCompleted="Grid_ManipulationCompleted">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Image Source="{Binding Path=IconSource}"
               Grid.Column="0"
               Width="96"
               Height="96"
               VerticalAlignment="Center"
               Visibility="{Binding Path=Editing, Converter={StaticResource visibilityConverter}}"
               />

        <TextBlock Text="{Binding Path=Name}" Grid.Column="1" />

    </Grid>
</DataTemplate>

I’m binding the Visibility to a property of each EditableListItem, so I need to implement INotifyPropertyChanged so updates to the backing items are reflected in the UI. (Right? Or is there a simpler way to do it?)

EditableListItem:

public class EditableListItem : INotifyPropertyChanged
{
    private EditableListBox _parentListBox;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool Editing
    {
        get
        {
            return _parentListBox.Editing;
        }
    }

    public EditableListItem(Section section, EditableListBox parentListBox)
    {
        _parentListBox = parentListBox;

        // after this line, _parentListBox.PropertyChanged is still null.
        // why is that?
        _parentListBox.PropertyChanged += PropertyChanged;

        _parentListBox.PropertyChanged += new PropertyChangedEventHandler(_parentListBox_PropertyChanged);
    }

EditableListBox:

public partial class EditableListBox : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // NotifyPropertyChanged will raise the PropertyChanged event, 
    // passing the source property that is being updated.
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public void SetSectionsSource(ObservableCollection<Section> sectionsSource)
    {
        sectionsSource.CollectionChanged += new NotifyCollectionChangedEventHandler(sectionsSource_CollectionChanged);
        ContentListBox.ItemsSource = sectionsSource.Select(section => new EditableListItem(section, this) { Enabled = true });
        //ContentListBox.ItemsSource.Add(new EditableListItem(new Section("Section", 3)) { Enabled = true });
    }

    // ...

    private bool _editing;
    public bool Editing
    {
        get
        {
            return _editing;
        }
        set
        {
            _editing = value;
            NotifyPropertyChanged("Editing");
        }
    }

}

The Editing property is stored in EditableListBox – EditableListItem just forwards it. I wanted to attached EditableListItem.PropertyChanged to EditableListBox.PropertyChanged directly, but the following didn’t work:

  // after this line, _parentListBox.PropertyChanged is still null.
  // why is that?
  _parentListBox.PropertyChanged += PropertyChanged;

The following did work:

_parentListBox.PropertyChanged += new PropertyChangedEventHandler(_parentListBox_PropertyChanged);

Why is this? Is the first attempt totally invalid (if so, why does the compiler allow it?)?

  • 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-18T02:50:54+00:00Added an answer on May 18, 2026 at 2:50 am

    To begin with, you don’t wire up the PropertyChanged to implement it. The idea is that WPF uses that event and it wires it up. The only thing you do is trigger the event when applicable.

    And that’s a part of the issue here. You have the Editing property, but it is not being fired. I do understand that you have wired the PropertyChanged of the parent listbox to get the event to fire, but that is not going to work.

    If I get the idea right, what you want to accomplish is when the Editing property of the listbox gets changed, you want the PropertyChanged of the list item to be forced.

    One of the things of PropertyChanged is that the sender has to be the object where the PropertyChanged is located. This means that you should implement it like this:

    public partial class EditableListBox : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        // You really should make this protected. You do not want the outside world
        // to be able to fire PropertyChanged events for your class.
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        private bool _editing;
        public bool Editing
        {
            get
            {
                return _editing;
            }
            set
            {
                _editing = value;
                NotifyPropertyChanged("Editing");
            }
        }
    }
    
    public class EditableListItem : INotifyPropertyChanged
    {
        private EditableListBox _parentListBox;
    
        public EditableListItem(EditableListBox parentListBox)
        {
            _parentListBox = parentListBox;
    
            _parentListBox.PropertyChanged += new PropertyChangedEventHandler(_parentListBox_PropertyChanged);
        }
    
        void _parentListBox_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // Forward the event.
            if (e.PropertyName == "Editing")
                NotifyPropertyChanged("Editing");
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        // You really should make this protected. You do not want the outside world
        // to be able to fire PropertyChanged events for your class.
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public bool Editing
        {
            get
            {
                return _parentListBox.Editing;
            }
        }
    }
    

    I don’t know how you get the reference to the editable listbox, but lets say you get it via the constructor. When you get the reference, you attach the the PropertyChanged event handler of the listbox. Because, when the Editing property of that object changes, actually, your Editing property changes too. This is how you simulate that.

    One last thing: the reason why the PropertyChanged is still null after the += PropertyChanged is because the PropertyChanged of the object itself is null. You cannot wire the events in this way. The second way is the correct way of wiring up the events, and the above example shows what you do with this.

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

Sidebar

Related Questions

I have a curious case of Tcl that perhaps I just don't understand. The
Perhaps it's a syntax error, but I never assume that. I have a -dead-
Perhaps I'm just being dense, but I don't understand why Netbeans is telling me
So perhaps I've found a bug in Rails 3.1.1, or else I don't understand
Perhaps the title is worded incorrectly. I have a global Busy Indicator that works
I've got some code that I don't think is able to be multithreaded, perhaps
Perhaps I don't understand how clone() works. Shouldn't the return value equal the caller?
This appears to be strange behavior, or perhaps I don't understand regular expressions so
Perhaps making use of jniwrap.jar and winpack.jar so I don't have to roll my
I don't know if I'm thinking of this the right way, and perhaps somebody

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.