Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM - ListBox SelectedItem Binding Property Going Null
    primarykey
    data
    text
    <p>So i have a listbox:</p> <pre><code>&lt;ListBox x:Name="listbox" HorizontalAlignment="Left" Margin="8,8,0,8" Width="272" BorderBrush="{x:Null}" Background="{x:Null}" Foreground="{x:Null}" ItemsSource="{Binding MenuItems}" ItemTemplate="{DynamicResource MenuItemsTemplate}" SelectionChanged="ListBox_SelectionChanged" SelectedItem="{Binding SelectedItem}"&gt; &lt;/ListBox&gt; </code></pre> <p>and i have this included in my viewmodel:</p> <pre><code> public ObservableCollection&lt;MenuItem&gt; MenuItems { get { return menuitems; } set { menuitems = value; NotifyPropertyChanged("MenuItems"); } } public MenuItem SelectedItem { get { return selecteditem; } set { selecteditem = value; NotifyPropertyChanged("SelectedItem"); } } </code></pre> <p>and also in my viewmodel:</p> <pre><code> public void UpdateStyle() { ActiveHighlight = SelectedItem.HighlightColor; ActiveShadow = SelectedItem.ShadowColor; } </code></pre> <p>So, the objective is to call UpdateStyle() whenever selectedchanged event is fired. So in the .CS file, i call UpdateStyle(). The problem is, whenever I get into the selectionchanged event method, my ViewModel.SelectedItem is always null. I tried debugging this to see if the binding was working correctly, and it is. When I click on an item in the listbox, the SelectedItem Set is triggered, setting the value... but somewhere inbetween that and the selected changed (In the CS File) It gets reset to Null.</p> <p>Can anyone help out?</p> <p>Thanks </p> <p>Edit: I thought I might shed a little more light. 1. Click on an item in the list 2. SelectedItem.Set gets triggered, ViewModel.SeletedItem gets set correctly. 3. Enter the OnSelectionChanged Event in the .CS file. 4. Enter ViewModel.UpdateStyle() 5. SelectedItem Throws a Null Exception.</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.
 

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