Background:
Wrote a CS tool, want to open a folder in the client browser window to display the server-side folder.
Problem:
To the ready-made folderbrowserdialog somehow applied to this scene, had to write a simple window. Server-side WCF, which wants to send XML to the client from the server side, is displayed in the client with the TreeView.
Code:
Xaml:
<Windowx:class= "Wpfapplication1.myfolderbrowserwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "Browse Server folder"Height= "+"Width= "+"ResizeMode= "Noresize"windowstartuplocation= "Centerowner"> <window.resources> <hierarchicaldatatemplatex:key= "ItemTemplate"ItemsSource="{Binding Xpath=folder}"> <TextBlockText="{Binding [email protected]}"/> </hierarchicaldatatemplate> </window.resources> <Grid> <TreeViewName= "TreeView"ItemsSource="{Binding}"treeviewitem.expanded= "treeview_expanded"> <treeview.itemtemplate> <hierarchicaldatatemplateItemsSource="{Binding Xpath=folder}"> <StackPanelOrientation= "Horizontal"Margin= "2"> <ImageSource= "Folder2.png"Width= "+"Height= "+"Snapstodevicepixels= "True"/> <TextBlockText="{Binding [email protected]}"Margin= "5,0"/> </StackPanel> </hierarchicaldatatemplate> </treeview.itemtemplate> </TreeView> </Grid></Window>
C#:
Before the window is opened, the server side first begins with a string, including the server name and XML that consists of the System.IO.DriveInfo.GetDrives (). Then each time a level is expanded, the triggering event gets a list of subfolders at the next level. (in order to have that triangle ...) )
Public Partial classMyfolderbrowserwindow:window {//string format: <folder name=\ "servername\" ><folder name=\ "\ C" Fullpath=\ "c:\\\" ></Folder></Folder> Public stringrootfolders {Get;Set; } PublicMyfolderbrowserwindow () {InitializeComponent (); This. Loaded + =window2_loaded; } voidWindow2_loaded (Objectsender, RoutedEventArgs e) {XmlDataProvider XDP=NewXmlDataProvider (); Xdp. XPath="Folder"; XmlDocument xd=NewXmlDocument (); Xd. LOADXML (rootfolders); Xdp. Document=xd; This. TreeView.Items.Clear ();//items and Itemsource can only have one effect, want to use one, the other must be empty, but why items are not empty? This. Treeview.datacontext =XDP; } Private voidTreeview_expanded (Objectsender, RoutedEventArgs e) {TreeViewItem Item= E.originalsource asTreeViewItem; if(Item! =NULL) { if(item. Hasitems) {foreach(varChildIteminchitem. Items) {XmlElement element= ChildItem asXmlElement; if(!element. HasChildNodes) {stringFullPath = element. GetAttribute ("FullPath"); Element. INNERXML= Getchildfolders (FullPath);//"<folder name=\" wpf\ "/><folder name=\" mfc\ "/><folder name=\" delphi\ "/>"; } } } } } Public Eventfunc<string,string>getchildfoldersevent; Public Delegate stringGetchildfolderseventhandler (Objectsender, Foldereventargs args); Public EventGetchildfolderseventhandler GetChildFoldersEvent2; Private stringGetchildfolders (stringFullPath) {Foldereventargs args=NewFoldereventargs (); Args. FullPath=FullPath; returnGetChildFoldersEvent2 ( This, args); //return getchildfoldersevent (FullPath); } } Public classFoldereventargs:eventargs { Public stringFullPath {Get;Set; } }
Results:
Reference:
Http://www.cnblogs.com/bray/archive/2010/04/13/1711333.html
http://blog.csdn.net/easybjy/article/details/44338515
PS: If there is a more convenient way, please tell it, thank you very much! (Write yourself a good problem ...) )
Using the TreeView to display XML in WPF