# Привязка (Binding). Интерфейс INotifyPropertyChanged. Форматирование значений привязки и конвертеры значений. ## Введение в привязку данных ``` ``` ![](./.img/scr1.png) ## Свойство Source ``` using System.Text; 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 WpfApp4 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } class Phone { public string Title { get; set; } public string Company { get; set; } public int Price { get; set; } } } ``` ``` ``` ![](./.img/scr2.png) ## Свойство TargetNullValue ``` ``` ![](./.img/scr3.png) ## Свойство RelativeSource ``` ``` ![](./.img/scr4.png) ## Свойство DataContext ``` ``` ![](./.img/scr5.png) # Форматирование значений привязки и конвертеры значений ## Форматирование значений ``` ``` ![](./.img/scr6.png) ## Конвертеры значений ``` using System.Globalization; using System.Text; 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 WpfApp4 { public class DateTimeToDateConverter : IValueConverter { public object Convert( object value, Type targetType, object parameter, CultureInfo culture) { return ((DateTime)value).ToString("dd.MM.yyyy"); } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } class Phone { public string Title { get; set; } public string Company { get; set; } public int Price { get; set; } } } ``` ``` 2/12/2016 ``` ``` using System.Globalization; using System.Text; 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 WpfApp4 { public class DateTimeToDateConverter : IValueConverter { public object Convert( object value, Type targetType, object parameter, CultureInfo culture) { if (parameter != null && parameter.ToString() == "EN") return ((DateTime)value).ToString("MM-dd-yyyy"); return ((DateTime)value).ToString("dd.MM.yyyy"); } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } class Phone { public string Title { get; set; } public string Company { get; set; } public int Price { get; set; } } } ``` ![](./.img/scr8.png)