Žiadny popis

sbakhtina 205f781950 Обновить 'README.md' 7 mesiacov pred
WpfApp2 eafeb46aa4 1st commit 7 mesiacov pred
img b8cf161ced Загрузить файлы 'img' 7 mesiacov pred
.gitignore.txt 4b0a6ae650 1st commit 8 mesiacov pred
README.md 205f781950 Обновить 'README.md' 7 mesiacov pred

README.md

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

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

<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>
        <StackPanel>
            <TextBox 
        x:Name="myTextBox" 
        Height="30" 
                Background="Pink"/>
            <TextBlock 
        x:Name="myTextBlock" 
        Text="{Binding 
                
            ElementName=myTextBox,
            Path=Text}"
        Height="30" 
                Background="LightSalmon"/>
        </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 WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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.Resources>
        <local:Phone 
            x:Key="nexusPhone" 
            Title="Nexus X5" 
            Company="Google" 
            Price="25000" />
    </Window.Resources>
    <Grid Background="PaleVioletRed">
        <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>

       Background="Pink">

   <StackPanel>
       <StackPanel.Resources>
           <sys:String 
           x:Key="ComboBoxTitle">Items:

           </sys:String>
       </StackPanel.Resources>
       <Label 
       Content="{StaticResource ComboBoxTitle}" 
           Background="LightCoral"/>
   </StackPanel>

Свойство TargetNullValue

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

Свойство RelativeSource

 <TextBox Text="{Binding 
 RelativeSource={RelativeSource Mode=Self}, 
 Path=Background, 
 Mode=TwoWay, 
 UpdateSourceTrigger=PropertyChanged}" 
 Background="PeachPuff"/>

Привязка к свойствам контейнера

    <Grid Background="Violet">
        <TextBlock 
        Foreground="Black"
        Text="{Binding 
            RelativeSource={RelativeSource 
                Mode=FindAncestor,
                AncestorType={x:Type Grid}}, 
            Path=Background}" />
    </Grid>

Свойство DataContext

    <Window.Resources>
        <local:Phone 
        x:Key="nexusPhone" 
        Title="Nexus X5" 
        Company="Google" 
        Price="25000" />
    </Window.Resources>
    <Grid 
    DataContext="{StaticResource nexusPhone}" 
   Background="Pink"
>
        <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.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}"
            Background="PaleVioletRed"/>
    </Grid>


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

<Window x:Class="WpfApp6.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:WpfApp6"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="DarkRed"
        >
    <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>

<Window x:Class="WpfApp6.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:WpfApp6"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Background="DarkRed"
        >
    <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}, 
            ConverterParameter=EN, 
            Converter={StaticResource myDateConverter}}" />
        <TextBlock 
        Text="{Binding 
            Source={StaticResource myDate}}" />
    </StackPanel>
</Window>