使用MVVM的WPF FolderBrowserDialog(后面没有代码)

时间:2019-01-08 09:33:06

标签: c# wpf mvvm

说明: 单击浏览按钮后,我必须获取FolderBrowserDialog Box。然后在FolderBrowserDialog框中,如果我选择任何文件夹并单击ok,则应该在浏览按钮旁边的文本框中显示特定的文件夹路径以及文件夹名称。...但是单击“浏览”按钮后我什么也没得到。 请检查我的代码并纠正我...

View.xaml

<Window....  xmlns:VM="clr-namespace:myproject.myViewModel"
...  >
<Window.DataContext><VM:myViewModel/>
<Grid>...
<TextBlock  Text="Folder to save files"  VerticalAlignment="Center" />
<TextBox   Text="{Binding Path=FoldernameWithPath  , UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"   Height="26"    IsReadOnly="True"  VerticalContentAlignment="Center"    Width="150"    />
 <Button      Content="Browse"    Height="26" VerticalAlignment="Bottom"  MinWidth="45"  Command="{Binding OpenFolderCommand}" />
</Grid>
</window> 

ViewModel.cs

    public ICommand OpenFolderCommand
    {
        get => new RelayCommand(a => this.OpenFolder(), p => CanOpenFolder());
    }
    private string _foldernameWithPath;
    public string FoldernameWithPath
    {
        get { return _foldernameWithPath; }
        set
        {
            if (value == _foldernameWithPath)
            {
                return;
            }
            else
            {
                _foldernameWithPath = value;
                OnPropertyChanged("FoldernameWithPath");
            }
        }
    }
    public bool CanOpenFolder()
    {
        return true;
    }
    private void OpenFolder()
    {
        FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
        if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && OpenFolderCommand.CanExecute(openFolderDialog.SelectedPath))
        {
            OpenFolderCommand.Execute(openFolderDialog.SelectedPath);
            FoldernameWithPath = openFolderDialog.SelectedPath;
        }
    }

3 个答案:

答案 0 :(得分:0)

我会这样写命令:

public ICommand OpenFolderCommand { get; private set; }
public MyViewModel() 
{
    this.OpenFolderCommand = new RelayCommand(a=> this.OpenFolder(),p=> CanOpenFolder());
}

答案 1 :(得分:0)

进行以下更改,

在View.xaml中

<Button Content="Browse" Command="{Binding OpenFolderCommand}"/>

在ViewModel.cs

public bool CanOpenFolder()
{
    return true;
}

private void OpenFolder()
{
    FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
    if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && OpenFolderCommand.CanExecute(openFolderDialog.SelectedPath))
    {
        //OpenFolderCommand.Execute(openFolderDialog.SelectedPath);
        FoldernameWithPath = openFolderDialog.SelectedPath;
    }
}

答案 2 :(得分:0)

您不应从委托的方法(OpenFolder())中调用命令。该命令除了在调用Execute时执行委托方法外,什么都不做,当您单击按钮时,该方法自动完成。

ICommand.CanExecute()通常也由WPF本身自动调用,并且基于仅启用,resp禁用按钮的结果。您很少在ViewModel中自己调用CanExecute。在您的情况下,您希望按钮始终处于启用状态,因此可以跳过CanExecute或使用p => true表达式。

这应该有效

xaml:

<TextBox Text="{Binding Path=FoldernameWithPath}" IsReadOnly="True" />
<Button Content="Browse"  Command="{Binding OpenFolderCommand}" />

viewmodel:

public ICommand OpenFolderCommand {get;} = new RelayCommand(p => OpenFolder());

private string _foldernameWithPath;
public string FoldernameWithPath
{
    get { return _foldernameWithPath; }
    set
    {
        if (value == _foldernameWithPath) return
        _foldernameWithPath = value;
        OnPropertyChanged("FoldernameWithPath");
    }
}

public void OpenFolder()
{
   FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
   if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
   {
      FoldernameWithPath = openFolderDialog.SelectedPath;
   }
}