LoginSignup
6
5

More than 5 years have passed since last update.

WPF > フォルダ選択

Last updated at Posted at 2018-09-12

背景

WPFにはフォルダ選択コントロールが無い

準備

WindowsAPICodePackを使う模様

nuget で WindowsAPICodePack-Shell を検索

image

ダウンロード数が多く、バージョンNOが高い版を選択。(作成者:Aybe)

WindowsAPICodePack-Core とWindowsAPICodePack-Shell がインストールされます。
image

使い方

XAML

ボタンとテキストボックスを配置します。

<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="564,84,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <TextBox Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="61,85,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="481"/>
</Grid>

コードビハインド(C#)


using Microsoft.WindowsAPICodePack.Dialogs;


/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var dlg = new CommonOpenFileDialog("フォルダ選択");
        dlg.IsFolderPicker = true;
        var ret = dlg.ShowDialog();
        if (ret == CommonFileDialogResult.Ok)
        {
            TextBox1.Text = dlg.FileName;
        }
    }
}



結果

フォルダ選択ダイアログが出ました。

image

参考

wpfでvsのフォルダ選択ダイアログみたいなダイアログを表示したい - Qiita

6
5
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
6
5