locked
Set Selected Item to Null in Listbox RRS feed

  • Question

  • Hi All,

    I have list in my WPF App.. where i change the list item source on TAB selection changes..

    I need to set Selected item to null when i change the tab.

    Currently first item of the list is getting selected when i change the item source of listbox.

    Am Using MVVM.

            private ObservableCollection<ShortCuts> lstShortCuts { get; set; }
            public ObservableCollection<ShortCuts> LstShortCuts
            {
                get { return lstShortCuts; }
                set
                {
                    lstShortCuts = value;
                    if (PropertyChanged != null)
                    {
                        
                        PropertyChanged(this, new PropertyChangedEventArgs("LstShortCuts"));
                    }
                }
            }
    
     private ShortCuts selectedShortCut { get; set; }
            public ShortCuts SelectedShortCut
            {
                get { return selectedShortCut; }
                set
                {
              
                   
                        selectedShortCut = value;
                        if (PropertyChanged != null)
                        {
                            PropertyChanged(this, new PropertyChangedEventArgs("SelectedShortCut"));
                        }
    
                        if (SelectedShortCutChanged != null && SelectedShortCut != null)
                        {
                            SelectedShortCutChanged(this, new GenericEventArgs<ShortCuts>(SelectedShortCut));
                        }
                                 
                }
            }
    

    <CollectionViewSource x:Key="shortCutGrouping" Source="{Binding LstShortCuts}">
                    <CollectionViewSource.GroupDescriptions>
                        <PropertyGroupDescription PropertyName="Group" />
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource> 
    
    <ListBox ItemTemplate="{StaticResource ShortCutLinkDataTemplate}" Background="Transparent" Grid.Row="1" BorderBrush="Transparent" ItemsSource="{Binding Source={StaticResource shortCutGrouping}}" Margin="5,0,5,5" SelectedItem="{Binding SelectedShortCut,Mode=TwoWay}"/>
    Any Suggestion would be of great Help


    Arjun

    Monday, July 18, 2016 1:46 PM

Answers

  • In your code that changes the collection you would have to 

    1. Set the collection

    2. Set the SelectedShortCut  to null


    Lloyd Sheen

    • Marked as answer by DotNet Wang Monday, August 1, 2016 6:35 AM
    Tuesday, July 19, 2016 10:13 PM

All replies

  • Instead of setting the selected value to null, set the selected index to -1. That way, it unselects the combo box item.

    Robert Johnston

    Monday, July 18, 2016 4:09 PM
  • Since you have the SelectedItem of the ListBox bound to SelectedShortCut simply set that property to Null although I notice you have a lot of code in the setter of this property.

    Lloyd Sheen

    Monday, July 18, 2016 4:24 PM
  • I have done that but still when i reset the collection it sets selected item to first item of the collection.

    Arjun

    Tuesday, July 19, 2016 7:09 AM
  • Not seeing all your code the one thing that could have happened is that you have code that breaks the binding between SelectedShortCut and the SelectedItem of the ListBox. 

    Try putting a breakpoint where you set the property to null.  After you hit that breakpoint put a breakpoint in the get of SelectedShortCut.  If that breakpoint is not called then that is your problem.


    Lloyd Sheen

    Tuesday, July 19, 2016 1:18 PM
  • I have tried putting brake point in setter of LstShortCuts 
    after property changes event it sets selected item to first item in list


    Arjun

    Tuesday, July 19, 2016 2:35 PM
  • In your code that changes the collection you would have to 

    1. Set the collection

    2. Set the SelectedShortCut  to null


    Lloyd Sheen

    • Marked as answer by DotNet Wang Monday, August 1, 2016 6:35 AM
    Tuesday, July 19, 2016 10:13 PM