Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET: Difficulty with Events
    primarykey
    data
    text
    <p>Perhaps I don't understand events fully.</p> <p>I'm building a Windows Phone 7 app in Silverlight.</p> <p>I have a <code>UserControl</code> that wraps a <code>ListBox</code>, called <code>EditableListBox</code>. The <code>ListBox</code> has a data template. The items in the list box are wrapped by <code>EditableListItem</code> objects.</p> <p>The data template is as follows:</p> <pre><code>&lt;DataTemplate&gt; &lt;Grid ManipulationCompleted="Grid_ManipulationCompleted"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Image Source="{Binding Path=IconSource}" Grid.Column="0" Width="96" Height="96" VerticalAlignment="Center" Visibility="{Binding Path=Editing, Converter={StaticResource visibilityConverter}}" /&gt; &lt;TextBlock Text="{Binding Path=Name}" Grid.Column="1" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; </code></pre> <p>I'm binding the <code>Visibility</code> to a property of each <code>EditableListItem</code>, so I need to implement <code>INotifyPropertyChanged</code> so updates to the backing items are reflected in the UI. (Right? Or is there a simpler way to do it?)</p> <p><code>EditableListItem</code>:</p> <pre><code>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); } </code></pre> <p><code>EditableListBox</code>:</p> <pre><code>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&lt;Section&gt; sectionsSource) { sectionsSource.CollectionChanged += new NotifyCollectionChangedEventHandler(sectionsSource_CollectionChanged); ContentListBox.ItemsSource = sectionsSource.Select(section =&gt; 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"); } } } </code></pre> <p>The <code>Editing</code> property is stored in <code>EditableListBox</code> - <code>EditableListItem</code> just forwards it. I wanted to attached <code>EditableListItem.PropertyChanged</code> to <code>EditableListBox.PropertyChanged</code> directly, but the following didn't work:</p> <pre><code> // after this line, _parentListBox.PropertyChanged is still null. // why is that? _parentListBox.PropertyChanged += PropertyChanged; </code></pre> <p>The following did work:</p> <pre><code>_parentListBox.PropertyChanged += new PropertyChangedEventHandler(_parentListBox_PropertyChanged); </code></pre> <p>Why is this? Is the first attempt totally invalid (if so, why does the compiler allow it?)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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