一旦我单击浏览按钮,我必须获取FolderBrowserDialog,但我应该通过使用MVVM来实现它。
解释:
-
获得文件夹浏览器对话框后,我应该能够选择要在其中保存文件的文件夹。
-
然后选择文件夹后,它应该向我显示选定的文件夹路径以及文件夹名称 文本框 在我的浏览按钮旁边的文本框中。
我怎样才能做到这一点....
您需要了解绑定。
在这个简单的示例中,我添加了一个绑定到命令的按钮 - 它替换了事件背后的代码。我也在Nuget中使用了现有的IComman并实现它(以及许多其他功能) - Nuget的名称是Prism6.MEF。
这是一个例子:
Xaml:
<Grid>
<StackPanel>
<TextBlock Text="{Binding BindableTextProperty}" />
<Button Content ="Do Action" Command="{Binding DoAction}" Height="50"/>
</StackPanel>
</Grid>
代码隐藏:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
查看型号 :
class MainWindowVM : BindableBase
{
private string m_bindableTextProperty;
public MainWindowVM()
{
DoAction = new DelegateCommand(ExecuteDoAction,CanExecuteDoAction);
}
public string BindableTextProperty
{
get { return m_bindableTextProperty; }
set { SetProperty(ref m_bindableTextProperty , value); }
}
public DelegateCommand DoAction
{
get;
private set;
}
private bool CanExecuteDoAction()
{
return true;
}
private void ExecuteDoAction()
{
// Do something
// You could enter the Folder selection code here
BindableTextProperty = "Done";
}
}
正如我在开始时解释的那样,要理解它为什么有效,您必须了解 WPF 中的绑定,尤其是 INotifyPropertyChange - 对于文本块上的数据
希望它对:)有所帮助