# Привязка (Binding). Интерфейс INotifyPropertyChanged. Форматирование значений привязки и конвертеры значений.
## Введение в привязку данных
```
```
![](./img/1.jpg)
## Режимы привязки
```
```
![](./img/2.jpg)
## Свойство 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 binding
{
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/3.jpg)
```
Мяв:
```
![](./img/4.jpg)
## Свойство TargetNullValue
```
```
![](./img/5.jpg)
## Свойство RelativeSource
```
```
![](./img/6.jpg)
```
```
![](./img/7.jpg)
## Свойство DataContext
```
```
![](./img/8.jpg)
# Форматирование значений привязки и конвертеры значений
## Форматирование значений
```
```
![](./img/9.jpg)
```
```
![](./img/10.jpg)
```
```
![](./img/11.jpg)
## Конвертеры значений
```
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 binding
{
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
```
![](./img/12.jpg)
```
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 binding
{
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/13.jpg)