PageRenderTime 84ms CodeModel.GetById 24ms RepoModel.GetById 2ms app.codeStats 0ms

/Visual Studio 2008/CSVSPackageWPFToolWindow/ReadMe.txt

#
Plain Text | 210 lines | 165 code | 45 blank | 0 comment | 0 complexity | 77adde2e1f7527b0e443bbf76947328d MD5 | raw file
  1. =============================================================================
  2. VSX Module: CSVSPackageWPFToolWindow Project Overview
  3. =============================================================================
  4. /////////////////////////////////////////////////////////////////////////////
  5. Use:
  6. This sample demostrate how to host a WPF control into Visual Studio
  7. ToolWindow.
  8. To use the sample
  9. 1. Run the sample
  10. 2. Open tool window by View / Other Windows / WPFToolWindow
  11. 3. A tool window will be docked at place of solution explorer
  12. 4. The tool window hosts a WPF control which represents MyDocuments folder
  13. structure
  14. //////////////////////////////////////////////////////////////////////////////
  15. Prerequisites:
  16. VS 2008 SDK must be installed on the machine. You can download it from:
  17. http://www.microsoft.com/downloads/details.aspx?FamilyID=30402623-93ca-479a-867c-04dc45164f5b&displaylang=en
  18. Otherwise the project may not be opened by Visual Studio.
  19. If you run this project on a x64 OS, please also config the Debug tab of the project
  20. Setting. Set the "Start external program" to
  21. C:\Program Files(x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe
  22. NOTE: The Package Load Failure Dialog occurs because there is no PLK(Package Load Key)
  23. Specified in this package. To obtain a PLK, please to go to WebSite:
  24. http://msdn.microsoft.com/en-us/vsx/cc655795.aspx
  25. More info
  26. http://msdn.microsoft.com/en-us/library/bb165395.aspx
  27. /////////////////////////////////////////////////////////////////////////////
  28. Creation:
  29. 1. Creating the Project
  30. Create a Visual Studio Integration Package project that provides a tool window.
  31. Later, you add a WPF control to this tool window.
  32. To create the Visual Studio project
  33. Create a Visual Studio package that provides a tool window.
  34. In the Visual Studio Integration Package Wizard, use the following settings:
  35. - Set the programming language to Visual Basic or Visual C#.
  36. - Use the default values on the Basic Package Information page.
  37. - Add a tool window that is named Hosted WPF Clock Control.
  38. - Click Finish.
  39. The wizard generates a project that contains a WinForms user control,
  40. MyControl, for the tool window.
  41. 2. Adding the WPF User Control
  42. Add a WPF user control to your project. The user control presented here
  43. represents MyDocuments folder structure by tree view.
  44. Then, add this WPF control to the control for the tool window in your package.
  45. To create the WPF user control
  46. In Solution Explorer, right-click the tool window project,
  47. point to Add, and then click New Item.
  48. In the Add New Item dialog box, select the User Control (WPF) template,
  49. name it WPFControl, and click Add.
  50. Open the WPFControl.xaml file for editing, and replace the child element
  51. of the UserControl element by using the following markup.
  52. XAML
  53. <UserControl x:Class="Company.VSPackageWPFToolWindow.WPFControl"
  54. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  55. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  56. Height="300" Width="300">
  57. <Grid>
  58. <TreeView Name="treeView" HorizontalAlignment="Stretch"
  59. HorizontalContentAlignment="Left" />
  60. </Grid>
  61. </UserControl>
  62. To open the code-behind file for editing, right-click in the XAML editor,
  63. and then click View Code.
  64. Add the following code:
  65. public TreeView WPFTreeView
  66. {
  67. get
  68. {
  69. return this.treeView;
  70. }
  71. }
  72. Save all files and verify that the solution builds.
  73. To add the WPF user control to the tool window
  74. a. Open the MyControl user control in design mode.
  75. b. Remove the default button, button1, from the control.
  76. c. In the Toolbox, directly drag WPFControl into the Windows Form
  77. Click Dock in parent container to set the Dock property to Fill.
  78. Right-click the design surface, and then click View Code to open the MyControl class in the code editor.
  79. Delete the button1_Click method that the wizard generated.
  80. Add the methods shown in the following example.
  81. public WPFControl WPFControl
  82. {
  83. get
  84. {
  85. return wpfControl1;
  86. }
  87. }
  88. Open the MyToolWindow.cs file, add following code:
  89. /// <summary>
  90. /// Standard constructor for the tool window.
  91. /// </summary>
  92. public MyToolWindow() :
  93. base(null)
  94. {
  95. // Set the window title reading it from the resources.
  96. this.Caption = Resources.ToolWindowTitle;
  97. // Set the image that will appear on the tab of the window frame
  98. // when docked with an other window
  99. // The resource ID correspond to the one defined in the resx file
  100. // while the Index is the offset in the bitmap strip. Each image in
  101. // the strip being 16x16.
  102. this.BitmapResourceID = 301;
  103. this.BitmapIndex = 1;
  104. control = new MyControl();
  105. wpfControl = control.WPFControl;
  106. }
  107. public override void OnToolWindowCreated()
  108. {
  109. base.OnToolWindowCreated();
  110. InitializeTreeViewContent();
  111. }
  112. private void InitializeTreeViewContent()
  113. {
  114. DirectoryInfo myDocInfo =
  115. new DirectoryInfo(
  116. Environment.GetFolderPath(
  117. Environment.SpecialFolder.MyDocuments
  118. )
  119. );
  120. IntializeTreeViewContentRecursively(myDocInfo, wpfControl.WPFTreeView.Items);
  121. }
  122. private void IntializeTreeViewContentRecursively(
  123. DirectoryInfo myDocInfo, ItemCollection itemCollection)
  124. {
  125. if (myDocInfo == null)
  126. return;
  127. try
  128. {
  129. TreeViewItem item = new TreeViewItem();
  130. item.Header = myDocInfo.Name;
  131. itemCollection.Add(item);
  132. DirectoryInfo[] subDirs = myDocInfo.GetDirectories();
  133. if (subDirs != null)
  134. {
  135. foreach (DirectoryInfo dir in subDirs)
  136. IntializeTreeViewContentRecursively(dir, item.Items);
  137. }
  138. FileInfo[] files = myDocInfo.GetFiles();
  139. if (files != null)
  140. {
  141. foreach (FileInfo file in files)
  142. {
  143. TreeViewItem fileItem = new TreeViewItem();
  144. fileItem.Header = file.Name;
  145. fileItem.Tag = file.FullName;
  146. item.Items.Add(fileItem);
  147. }
  148. }
  149. }
  150. catch
  151. {
  152. return;
  153. }
  154. }
  155. 3. Add regsitry attribute to the package
  156. Open CSVSPackageWPFToolWindowPackage.cs, add following attribute:
  157. [ProvideToolWindow(typeof(MyToolWindow), Style = VsDockStyle.Tabbed,
  158. Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057")]
  159. The attribute indicates that the tool window will be docked at the same place
  160. as solution explorer.
  161. /////////////////////////////////////////////////////////////////////////////
  162. References:
  163. Walkthrough: Hosting a WPF User Control in a Tool Window
  164. http://msdn.microsoft.com/en-us/library/cc826120.aspx
  165. /////////////////////////////////////////////////////////////////////////////