LoginSignup
17
21

More than 5 years have passed since last update.

WPFでファイルやフォルダのパスをドラッグandドロップでtextboxに入力するようにする

Last updated at Posted at 2014-02-06

細かい点かもしれませんが、こういうのがあると便利だったりします。
textBoxという名前のUIがあったとすると、そのイベントTextBox_PreviewDragOverとTextBox_Dropに以下のメソッドを登録します。

private void textBox_PreviewDragOver(object sender, System.Windows.DragEventArgs e) {
    if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop, true)) {
        e.Effects = System.Windows.DragDropEffects.Copy;
    } else {
        e.Effects = System.Windows.DragDropEffects.None;
    }
    e.Handled = true;
}
private void textBox_Drop(object sender, System.Windows.DragEventArgs e) {
    var dropFiles = e.Data.GetData(System.Windows.DataFormats.FileDrop) as string[];
    if (dropFiles == null) return;
    textBox.Text = dropFiles[0];
}

これで、パスの入力が簡単になります。

17
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
21