Note that there are some explanatory texts on larger screens.

plurals
  1. POWinforms DataBinding vs PropertyChanged Event Ordering Issue
    primarykey
    data
    text
    <p>I'm sorry if this has been asked before, but I did try digging through the multitude of DataBinding questions available and couldn't find one that matched my problem.</p> <p>I'm doing a unidirectional databinding between a listbox index and some property on one of my entities, <em>but</em> I also need to run some additional logic <em>after</em> the databinding takes place. I tried doing it like this:</p> <pre class="lang-cs prettyprint-override"><code>listBox.DataBindings.Add("SelectedIndex", entity, "Property", true, DataSourceUpdateMode.OnPropertyChanged); listBox.SelectedIndexChanged += (s,e) =&gt; view.Refresh(); </code></pre> <p>The problem is that the event handler runs <em>before</em> the databinding updates, but <code>view.Refresh()</code> depends on the value of <code>entity.Property</code> being updated. This results in my view not being refreshed until the the next time I change my index.</p> <p>Is there any way to solve this <em>with</em> databinding? At the moment I've resorted to not using databinding whenever I have this scenario, and doing it manually instead:</p> <pre class="lang-cs prettyprint-override"><code>listBox.SelectedIndex = entity.Property; listBox.SelectedIndexChanged += OnIndexChanged; private void OnIndexChanged(object sender, EventArgs e) { entity.Property = listBox.SelectedIndex; view.Refresh(); } </code></pre> <p>But I'd prefer to avoid this since I'm using databinding for everything else and would like to keep consistency.</p> <hr> <p><strong>Update</strong></p> <p>I've found the solution. It looks like the Binding object itself holds BindingComplete event which is perfect for this. And since the DataBindings.Add method returns me the Binding object I can chain both together. My code therefore becomes:</p> <pre class="lang-cs prettyprint-override"><code>listBox.DataBindings.Add("SelectedIndex", entity, "Property", true, DataSourceUpdateMode.OnPropertyChanged).BindingComplete += (s,e) =&gt; view.Refresh(); </code></pre> <p>My only question now is if need to take care of unregistering this event manually, or if I can just call <code>listBox.DataBindings.Clear();</code> and be done with 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.
 

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