Вам нужно узнать о связывании.
В этом простом примере я добавил кнопку, которая привязывается к команде - заменяет код, стоящий за событием. Я также использовал в Nuget, который существует в ICommand и реализуетэто (наряду с множеством других функций) - имя 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
Надеюсь, это помогло:)