Html
Файл 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>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<!-- textbox with binding text-->
<TextBox Text="{Binding BookName}" Grid.Column="0" Height="25" Width="250" />
<!-- button-->
<Button Content="Click me" Grid.Column="1" Height="25" Width="100" Click="MyButtonClick1" />
</Grid>
</Window>
Файл MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// for binding
this.DataContext = this;
}
private string _bookName = "Lords of the rings";
public string BookName
{
get
{
return _bookName;
}
set { _bookName = value; }
}
private void MyButtonClick1(object sender, RoutedEventArgs e)
{
MessageBox.Show(_bookName);
}
}
}