Question

So I'm using FileSystemWatcher to populate and update a playlist. I want to replicate many features of Windows Explorer, most importantly:
* inline rename
* slow double click to rename

I'm having quite a hassle doing this, so I thought, maybe there's an easier way than reimplementing the wheel? Maybe I can somehow host a Windows Explorer window in my application as a control?

Was it helpful?

Solution

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:

  1. Use Microsoft.Win32.OpenFileDialog if it can be easily adapted to your need, or
  2. Code your own functionality, or if you are very brave:
  3. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top