Добавим TextBlock в файл MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding BookName}"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0 20 0 0"/>
</Grid>
</Window>
Добавим код в файл MainWindow.xaml.cs
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public string BookName { get; set; }
public MainWindow()
{
BookName = "King of monkey";
InitializeComponent();
// for binding
this.DataContext = this;
}
}
}
Поменяем файл MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding BookName}"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0 20 0 0"/>
<Button
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0 50 0 0"
Content="Set Hello"
Command="{Binding ClickMyButton1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Width="142"/>
</Grid>
</Window>
Добавим код в файл MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
class ButtonCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action<object> _callback;
public ButtonCommand(Action<object> callback)
{
_callback = callback;
}
public virtual void Execute(object parameter) { _callback(parameter); }
public virtual bool CanExecute(object parameter) { return true; }
}
public partial class MainWindow : Window
{
public string BookName { get; set; }
public ICommand ClickMyButton1 { get; set; }
public MainWindow()
{
BookName = "King of monkey";
// button handler
ClickMyButton1 = new ButtonCommand((parameter) => {
BookName = "Hello";
});
InitializeComponent();
// for binding
this.DataContext = this;
}
}
}
Поменяем файл MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding MyModel.BookName}"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0 20 0 0"/>
<Button
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0 50 0 0"
Content="Set Hello"
Command="{Binding MyModel.ClickMyButton1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Width="142"/>
</Grid>
</Window>
Добавим код в файл MainWindow.xaml.cs
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
public class ModelInfo : NotifyPropertyChanged
{
private string _bookName;
public string BookName
{
get => _bookName;
set => SetProperty(ref _bookName, value);
}
public ICommand ClickMyButton1 { get; set; }
public ModelInfo()
{
BookName = "King of monkey";
// button handler
ClickMyButton1 = new ButtonCommand((parameter) => {
BookName = "Hello";
});
}
}
public partial class MainWindow : Window
{
public ModelInfo MyModel { get; }
public MainWindow()
{
MyModel = new ModelInfo();
InitializeComponent();
// for binding
this.DataContext = this;
}
}
///// Base functionality /////
public class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
protected virtual bool SetProperty<T>(ref T field, T value, [CallerMemberName] string PropertyName = null)
{
if (Equals(field, value)) return false;
field = value;
OnPropertyChanged(PropertyName);
return true;
}
}
class ButtonCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action<object> _callback;
public ButtonCommand(Action<object> callback)
{
_callback = callback;
}
public virtual void Execute(object parameter) { _callback(parameter); }
public virtual bool CanExecute(object parameter) { return true; }
}
////////////
}