2

So i have a listbox:

<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}">

</ListBox>

and i have this included in my viewmodel:

    public ObservableCollection<MenuItem> MenuItems
    {
        get
        {
            return menuitems;
        }
        set
        {
            menuitems = value;
            NotifyPropertyChanged("MenuItems");
        }
    }
    public MenuItem SelectedItem
    {
        get
        {
            return selecteditem;
        }
        set
        {
            selecteditem = value;
            NotifyPropertyChanged("SelectedItem");
        }
    }

and also in my viewmodel:

    public void UpdateStyle()
    {
        ActiveHighlight = SelectedItem.HighlightColor;
        ActiveShadow = SelectedItem.ShadowColor;
    }

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.

Can anyone help out?

Thanks

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.

Peanut
  • 2,126
  • 4
  • 25
  • 38
  • A similar question has been asked before that should help you out. http://stackoverflow.com/questions/414074/twoway-manual-binding-implementation-for-listbox-selecteditems – jsmith May 27 '10 at 02:39

2 Answers2

2

Wow, found a strange issue:

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}" d:DataContext="{d:DesignData /SampleData/MainViewModelSampleData.xaml}">

That code is generated by Expression Blend - and it was causing the issue. I erased all generated binding and just made a this.datacontext a new VM in the constructor of the XAML... now its working.

Thanks anyway, guys.

Peanut
  • 2,126
  • 4
  • 25
  • 38
  • That code is to provide sample data at designtime only. It's very handy. I know because I deleted it too to fix something and then realized later what it was and did. There should be code in your codebehind that sets the data context for runtime. – Nathanial Woolls Sep 14 '13 at 21:20
0

Look to see if your backing property (selecteditem) is getting set to NULL somewhere in your code.

Zamboni
  • 7,897
  • 5
  • 43
  • 52