[英]WPF OpenFileDialog using MVVM (Model-View-ViewModel) in c#
[英]WPF FolderBrowserDialog using Model-View-ViewModel (MVVM) in c#
單擊瀏覽按鈕后,我必須獲取FolderBrowserDialog,但應該使用MVVM來實現。
說明:
收到FolderBrowserDialog Box后,我應該能夠選擇要保存文件的文件夾。
然后,在選擇文件夾之后,它應該在我的瀏覽按鈕旁邊的文本框中顯示所選的文件夾路徑以及文件夾名稱。
我該如何實現呢?
您需要了解綁定。
在這個簡單的示例中,我添加了一個綁定到Command的按鈕-替換了event背后的代碼。我還使用了現存ICommand並實現它的Nuget(以及許多其他功能)-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-對於TextBlock上的數據
希望它有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.