Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To begin with, you don't wire up the <code>PropertyChanged</code> 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.</p> <p>And that's a part of the issue here. You have the <code>Editing</code> property, but it is not being fired. I do understand that you have wired the <code>PropertyChanged</code> of the parent listbox to get the event to fire, but that is not going to work.</p> <p>If I get the idea right, what you want to accomplish is when the <code>Editing</code> property of the listbox gets changed, you want the <code>PropertyChanged</code> of the list item to be forced.</p> <p>One of the things of <code>PropertyChanged</code> is that the sender <strong>has</strong> to be the object where the <code>PropertyChanged</code> is located. This means that you should implement it like this:</p> <pre><code>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; } } } </code></pre> <p>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 <code>PropertyChanged</code> event handler of the listbox. Because, when the <code>Editing</code> property of that object changes, actually, your <code>Editing</code> property changes too. This is how you simulate that.</p> <p>One last thing: the reason why the <code>PropertyChanged</code> is still <code>null</code> after the <code>+= PropertyChanged</code> is because the <code>PropertyChanged</code> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload