What's the best way to replicate the functionality of a Windows Explorer folder UI in WPF?
So I'm using FileSystemWatcher
to populate and update a playlist. I want to replicate many features of Windows Explorer, most importantly:
I'm having quite a hassle doing this, so I thought, maybe there's an easier w开发者_如何学Pythonay than reimplementing the wheel? Maybe I can somehow host a Windows Explorer window in my application as a control?
Hosting a real Windows Explorer window in your application is possible but fraught with peril: The techniques are different in XP vs Vista vs Win7 and you will be dealing with all sorts of low-level stuff. I would strongly recommend against trying it.
I think your best options are:
- Use Microsoft.Win32.OpenFileDialog if it can be easily adapted to your need, or
- Code your own functionality, or if you are very brave:
- Launch a separate Explorer window, optionally with code that tries to force its position and size to be over your application (this too is hard...)
Notes on inline rename feature
The inline rename and slow double-click to rename features are really quite trivial to implement.
In your view model add:
- A "NewName" string DependencyProperty
- A "Renaming" bool DependencyProperty with a PropertyChangedCallback. When "Renaming" goes true, copy Name to NewName. When it goes false, if NewName!=Name rename the file an update Name.
In your DataTemplate add a trigger on "Renaming" that replaces your TextBlock bound to "Name" with a TextBox bound to "NewName".
Add these event handlers: * KeyDown event: If F2 is pressed toggle Renaming. If Enter is pressed and Renaming, set Renaming=false. If Esc is pressed and Renaming copy Name to NewName and set Renaming=false. * LostFocus event: Set Renaming=false * SelectionChanged event on container: Record timestamp of last selection change. * MouseDown event: If left click and selection changed > 0.5 seconds ago, set Renaming = true
Many other features of Explorer view are similarly easy to implement, such as grouping and multiple columns.
Hope this helps.
精彩评论