locked
VS 2015 Extensibility Combo Update when dynamically removing items from list RRS feed

  • Question

  • I have a VS 2015 Extensibility project in which I have a combo list of items and a button. The button, opens a WPF tool window that allows the user to alter/add a new item to the combo list, or can remove the list.

    If I add an item to the list on my tool window, it successfully adds the item, also it can successfully remove the item. The problem is if I remove the currently selected item in the combo list does not refresh until the user clicks on it. This could cause other problems for the user because the entry selected is no longer in the list. How can I get the combo list to reflect the update. Below shows the package code that initializes the objects and where I am trying to refresh the combo.

    public MyPackageCommand( Package package, IMyService myService )
    {
        if( package == null )
        {
            throw new ArgumentNullException( "package" );
        }
    
        _package = package;
        _myService  = myService ;
    
        if( CommandService != null )
        {
            var dropDownCombo = new CommandID(   
                PackageGuids.guidMyServicePackageCmdSet, 
                (int)PackageIds.cmdIdCombo );
                var dropDownComboCommand = new OleMenuCommand( OnDropDownCombo, dropDownCombo );
                dropDownComboCommand.ParametersDescription = "$";
                CommandService.AddCommand( dropDownComboCommand );
    
    
            var dropDownComboList = new CommandID( 
                PackageGuids.guidMyServicePackageCmdSet, 
                (int)PackageIds.cmdIdComboGetList );
            var dropDownComboListCommand = new OleMenuCommand( OnDropDownComboList, dropDownComboList );
            CommandService.AddCommand( dropDownComboListCommand );
    
            var cmdEdit = new CommandID( 
                PackageGuids.guidMyServicePackageCmdSet, 
                (int)PackageIds.cmdIdEditor);
            var buttonCommand = new OleMenuCommand( ShowEditorWindow , cmdEdit );
            CommandService.AddCommand( buttonCommand );
        }
    
        _currentComboSelection = _myService.FindValueKey();
     }
     private void ShowEditorWindow( object sender, EventArgs e )
     {    
        ToolWindowPane window = _package.FindToolWindow( typeof(ToolWindowEditor), 0, true );
        if( (window == null) || (window.Frame == null) )
        {
            using (ResXResourceSet resource = new ResXResourceSet( @".\VSPackage.resx" ))
            {
                throw new NotSupportedException( resource.GetString( "CanNotCreateWindow" ) );                    
            }
        }            
        var control = window.Content as Editor;
    
        if( control != null )
        {
            var context = control.DataContext as EditorViewModel;
            if( context != null )
                context.MyEvent += OnMyEventHandler;
        }
    
        IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
        ErrorHandler.ThrowOnFailure( windowFrame.Show() );
    
    
    }
    
    private void OnMyEventHandler( object sender, EventArgs eventArgs )
    {
        // how do i get at the combo control here?   
    }

    The drop down combo event re-populates the list correctly but only when you click on it, but I need to do this before they click the drop down and I don't see how to get at the combo for my event handler. The code below is what works for the combo and explains better why I am unsure of updating the extensible combo box.

        private void OnDropDownComboList( object sender, EventArgs e )
        {
            var eventArgs = e as OleMenuCmdEventArgs;
    
            if( eventArgs == null )
                throw new ArgumentException( "Event arguments are required." );
    
            IntPtr outPtr = eventArgs.OutValue;
    
            if( outPtr != IntPtr.Zero )
            {
                _comboSelections = GetComboSelections();
                Marshal.GetNativeVariantForObject( _comboSelections, outPtr );
            }
            else
            {
                throw (new ArgumentException( "The output argument is required." ));
            }
    
        }
    Thanks in advance, Kent



    Wednesday, February 1, 2017 10:15 PM

Answers

  • It took a while to figure out, but it appears the underlying array and selection variable are connected like RoutedEventArgs to the package controls. The answer was to code this exactly as described above and not worry about the control update, simply refresh the data in my local event handler like this:

            private void OnMyEventHandler( object sender, MyEventArgs eventArgs )
            {
                _myService.Refresh();
                _comboSelections = GetComboSelections();
                _currentComboSelection = eventArgs.NewSelection;
            }
    

    The combo control connected to the _comboSelections and _currentComboSelection variables took care of the update when the event occurred.

    • Marked as answer by Phinque Thursday, February 2, 2017 7:12 PM
    Thursday, February 2, 2017 7:12 PM