none
Word Document Stealing Focus RRS feed

  • Question

  • I am having issues with setting focus on my WPF Action Pane and the Word Document Stealing Focus from the Action Pane. Any tips, article, examples / help greatly appreciated. I am calling a method that calls into the globals.thisdocument and call a method to update bookmarks of clear the bookmarks.

     

     

    private void UpdateBookmarks(MailMerges mail)
        {
          try
          {
            if (mail != null)
            {
              Globals.ThisDocument.UpdateBookmarks(mail);
            }
            else
            {
              Globals.ThisDocument.ClearBookmarks();
            }
          }
    
          catch (Exception ex)
          {
            new HandleException(ex);
            return;
          }
        }
    

    the UpdateBookmarks(MailMerges mail) and ClearBookmarks() simply set the this.bookmark.text = "Value" for each bookmark... All works fine except my focus jumps to the Word Document.

     

    Tuesday, August 23, 2011 3:56 PM

Answers

  • I decided to do it programatically. I just created a local variable to my view so I could reference it and set the focus from code. Works fine!

    Wednesday, August 24, 2011 3:06 AM

All replies

  • Please show us the code these procedures use. Also tell us the version of Word and VSTO involved.


    Cindy Meister, VSTO/Word MVP
    Tuesday, August 23, 2011 4:07 PM
    Moderator
  • It is a Word 2010 Document Level .Docx / VS 2010 / Target Framework is .Net 4.

     

    OK I will give you what relates my ThisDocument.cs creates instance of my ViewModel and stores it in a local private variable. On startup I create a new UserControl / Add an ElementHost / Passing my ViewModel.

     private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
          try
          {
            var action = new UserControl {Height = 800, Width = 400};
            action.Controls.Add(new ElementHost
                        {
                          Child = new MedsView(_viewModel),
                          Dock = DockStyle.Fill
                        });
    
            this.ActionsPane.Controls.Add(action);
          }
    
          catch (Exception ex)
          {
            new HandleException(ex);
            return;
          }
        }
    

       My WPF UserControl / View uses the Interactivity Extensions to Bind an Event to a DelegateCommand / Method on my ViewModel:

    <controls:FilteringComboBox x:Name="cmbMed" Margin="5,0" ItemsSource="{Binding ListOfMeds}" DisplayMemberPath="Value" ItemsPanel="{DynamicResource ItemsTemplate}" IsEditable="True" IsTextSearchEnabled="False">
              <i:Interaction.Triggers>
                <i:EventTrigger EventName="DropDownClosed" SourceObject="{Binding ElementName=cmbMed}">
                  <i:InvokeCommandAction Command="{Binding SelectionChangedMedCombo}" CommandParameter="{Binding ElementName=cmbMed, Path=SelectedItem}"/>
                </i:EventTrigger>
              </i:Interaction.Triggers>
            </controls:FilteringComboBox>
    

    There is a CanExecute and an Execute associated w/ the SelectionChangedMedCombo I will skip for now it checks the value and then Calls the Method to Update the Bookmarks in the Globals.ThisDocument.UpdateBookmarks():

    public void UpdateBookmarks(MailMerges mail) 
        {
          try
          {
            this.AddressBookmark.Text = mail.Address;
            this.CityBookmark.Text = mail.City;
            this.FirstBookmark.Text = mail.FirstName;
            this.LastBookmark.Text = mail.LastName;
            this.StateBookmark.Text = mail.State;
            this.TitleBookmark1.Text = mail.Title;
            this.Zip_Bookmark.Text = mail.Zip;
            this.todayBookmark.Text = DateTime.Today.ToShortDateString();
          }
    
          catch (Exception ex)
          {
            new HandleException(ex);
            return;
          }
        }
    


    After you select the ComboBox there are more entries on the Control the Word Document is Now the Focus and a Key Press will begin modifying the Document itself.

     

    Thanks!

     

     

    Tuesday, August 23, 2011 4:37 PM
  • I decided to do it programatically. I just created a local variable to my view so I could reference it and set the focus from code. Works fine!

    Wednesday, August 24, 2011 3:06 AM