No Description

Bolshakova Anna a9cb5743df first commit 7 months ago
.idea a9cb5743df first commit 7 months ago
AvaloniaApplication2 a9cb5743df first commit 7 months ago
img a9cb5743df first commit 7 months ago
AvaloniaApplication2.sln a9cb5743df first commit 7 months ago
README.md a9cb5743df first commit 7 months ago

README.md

Привязка (Binding). Интерфейс INotifyPropertyChanged. Форматирование значений привязки и конвертеры значений.

Введение в привязку данных

<Window x:Class="binding.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:binding"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <TextBox 
        x:Name="myTextBox" 
        Height="30" 
                Background="Pink"
                />
            <TextBlock 
        x:Name="myTextBlock" 
        Text="{Binding 
            ElementName=myTextBox,
            Path=Text}"
        Height="30" 
                Background="PaleVioletRed"/>
        </StackPanel>
    </Grid>
</Window>

Режимы привязки

<Window x:Class="binding.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:binding"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        >
    <Grid>
        <StackPanel>
            <TextBox 
        x:Name="textBox1" 
        Height="30"
        Background="Pink"/>
            <TextBox 
        x:Name="textBox2" 
        Height="30" 
        Background="PaleVioletRed"
        Text="{Binding 
            ElementName=textBox1, 
            Path=Text, 
            Mode=TwoWay}" />
        </StackPanel>
    </Grid>
</Window>

Свойство 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; }
    }
}
<Window x:Class="binding.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:binding"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        >
    <Window.Resources>
        <local:Phone 
            x:Key="nexusPhone" 
            Title="Nexus X5" 
            Company="Google" 
            Price="25000" />
    </Window.Resources>
    <Grid Background="LightPink">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock 
            Text="Модель:" 
            Foreground="White"/>
        <TextBlock 
            x:Name="titleTextBlock" 
            Text="{Binding 
                Source={StaticResource nexusPhone}, 
                Path=Title}"
            Foreground="White" 
            Grid.Column="1"/>
        <TextBlock 
            Text="Цена:" 
            Foreground="White" 
            Grid.Row="1"/>
        <TextBlock 
            x:Name="priceTextBlock" 
            Text="{Binding 
                Source={StaticResource nexusPhone}, 
                Path=Price}"
            Foreground="White" 
            Grid.Column="1" 
            Grid.Row="1"/>
    </Grid>
</Window>

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="Pink"
        >
    
    <StackPanel>
        <StackPanel.Resources>
            <sys:String 
            x:Key="ComboBoxTitle">Мяв:
                
            </sys:String>
        </StackPanel.Resources>
        <Label 
        Content="{StaticResource ComboBoxTitle}" />
    </StackPanel>
</Window>

Свойство TargetNullValue

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="PeachPuff"
        >
    <Window.Resources>
        <local:Phone 
        x:Key="nexusPhone" 
        Company="Google" 
        Price="25000" />
    </Window.Resources>
    <StackPanel>
        <TextBlock 
        x:Name="titleTextBlock"
        Text="{Binding 
            Source={StaticResource nexusPhone}, 
            Path=Title, 
            TargetNullValue=Текст по умолчанию}" />
    </StackPanel>
</Window>

Свойство RelativeSource

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="PeachPuff"
        >
    <TextBox Text="{Binding 
    RelativeSource={RelativeSource Mode=Self}, 
    Path=Background, 
    Mode=TwoWay, 
    UpdateSourceTrigger=PropertyChanged}" />
</Window>

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="Pink"
        >
    <Grid Background="Pink">
        <TextBlock 
        Foreground="White"
        Text="{Binding 
            RelativeSource={RelativeSource 
                Mode=FindAncestor,
                AncestorType={x:Type Grid}}, 
            Path=Background}" />
    </Grid>
</Window>

Свойство DataContext

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="Pink"
        >
    <Window.Resources>
        <local:Phone 
        x:Key="nexusPhone" 
        Title="Nexus X5" 
        Company="Google" 
        Price="25000" />
    </Window.Resources>
    <Grid 
    DataContext="{StaticResource nexusPhone}" 
   
>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock 
        Text="Модель" />
        <TextBlock 
        Text="{Binding Title}" 
        Grid.Row="1" />
        <TextBlock 
        Text="Производитель" 
        Grid.Column="1"/>
        <TextBlock 
        Text="{Binding Company}" 
        Grid.Column="1" 
        Grid.Row="1" />
        <TextBlock 
        Text="Цена" 
        Grid.Column="2" />
        <TextBlock 
        Text="{Binding Price}" 
        Grid.Column="2" 
        Grid.Row="1" />
    </Grid>
</Window>

Форматирование значений привязки и конвертеры значений

Форматирование значений

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="Pink"
        >
    <Window.Resources>
        <local:Phone 
        x:Key="nexusPhone" 
        Title="Nexus X5" 
        Company="Google" 
        Price="25000" />
    </Window.Resources>
    <Grid>
        <TextBlock 
        Text="{Binding 
            StringFormat=Итоговая цена {0} рублей, 
            Source={StaticResource nexusPhone}, 
            Path=Price}" />
    </Grid>
</Window>

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="Pink"
        >
    <Window.Resources>
        <local:Phone 
        x:Key="nexusPhone" 
        Title="Nexus X5" 
        Company="Google" 
        Price="25000" />
    </Window.Resources>
    <Grid>
        <TextBlock 
    Text="{Binding 
        StringFormat={}{0:C}, 
        Source={StaticResource nexusPhone}, 
        Path=Price}" />
    </Grid>
</Window>

<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="Pink"
        >
    <Window.Resources>
        <local:Phone 
        x:Key="nexusPhone" 
        Title="Nexus X5" 
        Company="Google" 
        Price="25000" />
    </Window.Resources>
    <Grid>
        <TextBlock 
    Text="{Binding 
        StringFormat={}{0:C}, 
        Source={StaticResource nexusPhone}, 
        Path=Price}" />
        <Button 
    ContentStringFormat="{}{0:C}"
    Content="{Binding 
        Source={StaticResource nexusPhone}, 
        Path=Price}" />
    </Grid>
</Window>

Конвертеры значений

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; }
    }
   
}
<Window x:Class="binding.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:binding"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="Pink"
        >
    <Window.Resources>
        <sys:DateTime x:Key="myDate">
            2/12/2016
        </sys:DateTime>
        <local:DateTimeToDateConverter 
        x:Key="myDateConverter" />
    </Window.Resources>
    <StackPanel>
        <TextBlock 
        Text="{Binding 
            Source={StaticResource myDate},
            Converter={StaticResource myDateConverter}}" />
        <TextBlock 
        Text="{Binding 
            Source={StaticResource myDate}}" />
    </StackPanel>
</Window>

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; }
    }
   
}