Žiadny popis

Jgrebnev f95e07f670 Обновить 'readme.md' 6 mesiacov pred
img a225ac60ca t8b 6 mesiacov pred
.gitignore.txt 62ee81afd3 1st commit 9 mesiacov pred
readme.md f95e07f670 Обновить 'readme.md' 6 mesiacov pred

readme.md

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

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

<Window x:Class="WpfApp2.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:WpfApp2"
        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="Lightblue"/>
        </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 WpfApp2
{
    /// <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="LightBlue">
        <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="Black" 
            Grid.Column="1"/>
        <TextBlock 
            Text="Цена:" 
            Foreground="DarkRed" 
            Grid.Row="1"/>
        <TextBlock 
            x:Name="priceTextBlock" 
            Text="{Binding 
                Source={StaticResource nexusPhone}, 
                Path=Price}"
            Foreground="DeepPink" 
            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="lightblue"
>
        <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="24999" />
    </Window.Resources>
    <Grid>
        <TextBlock 
        Text="{Binding 
            StringFormat=Итоговая цена {0} рублей, 
            Source={StaticResource nexusPhone}, 
            Path=Price}"
            Background="lightblue"/>
    </Grid>


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

<Window x:Class="WpfApp2.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:WpfApp2"
        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="WpfApp2.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:WpfApp2"
        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>