title: How to handle SelectedValue changing in ListBox correctly (WPF, C#)?
body: <p>I must handle <code>SelectedValue</code> property changing to refresh some dependent UI elements when a user selects some item in a <code>ListBox</code>. I use <code>SelectedValue</code> because I need to know a concrete value of the item, not an index in the <code>ListBox</code> (the index may be different under variuos circumstances) or something another. My <code>ListBox</code> is binded to a <code>DataTable</code> and <code>SelectedValue</code> contains ID of a row in the table. </p>
  
  <p>The problem is that when I try to get <code>SelectedValue</code> of the <code>ListBox</code> within <code>SelectionChanged</code> event handler, I receive a <strong>prior</strong> value, <strong>not the current</strong>! So, after I spent about a hour searching for ways of solving this problem, I noted that it is relatively well-known, but solutions I've seen do not seem to be very clear. Nevertheless, I "compiled" few ideas I have encountered to the next working snippet of code:</p>
  
  <pre><code>((DataRowView)this.LISTBOXNAME.SelectedItem)["ID"]
  </code></pre>
  
  <p>where <code>this</code> is my current window and <code>SelectedItem</code> may be casted to <code>DataRowView</code> as the <code>ListBox</code> is binded to a <code>DataTable</code> as I've mentioned before. This line of code gives a proper value of the selected <code>ListBoxItem</code>.</p>
  
  <p>But anyway, I still want to know <em>how to make <code>SelectedValue</code> get proper value directly</em>?  Is it possible? Or, maybe, must I use another event (not <code>SelectionChanged</code>)? And if it is impossible, can somebody explain me the way of thinking of kind guys from Microsoft and why that property has such a feature?</p>