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