Note that there are some explanatory texts on larger screens.

plurals
  1. POComboBox in WPF/MVVM with null and empty string
    text
    copied!<p>I have a problem with my ComboBox in WPF/MVVM project. After changing ViewModel (I use everything correctly: <code>INotifyPropertyChanged</code>, <code>ObservableCollection</code>, etc.) the ComboBox is not always changes its value. I've debugged it and the getter from ViewModel returned the correct value (<code>string.Empty</code>), but the ComboBox displayed a value from the list. A bit of code below:</p> <pre><code>// It doesn't work. &lt;ComboBox ItemsSource="{Binding ItemsCollection}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/&gt; // It works. &lt;TextBlock Text="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/&gt; </code></pre> <p>TextBlock is updated correctly if SelectedItem is <code>string.Empty</code> or <code>null</code>, but ComboBox is not updated correctly (old value stays) if SelectedItem is <code>string.Empty</code>. It's cleared only if SelectedItem is <code>null</code> value.<br> Why is this occurring?</p> <p>// <strong>EDIT</strong>:</p> <p>Maybe I explained it wrong. I have two grids: on the left I have tree with files and on the right I have properties of selected file. When I first select file <code>A</code>, which doesn't have setted property, the ComboBox is empty. If I choose file <code>B</code>, which have this property setted, it appers in the ComboBox. Next if I select file <code>C</code> with empty property, the old value stays in ComboBox (from before selected file - <code>B</code>). If this property from <code>C</code> is <code>null</code>, not <code>string.Empty</code>, ComboBox is cleared.</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