Geen omschrijving

ababin 9f2c7f42ee Обновить 'README.md' 5 maanden geleden
WpfApp1 701db76946 lab 5 maanden geleden
img e1f994e368 Загрузить файлы 'img' 5 maanden geleden
README.md 9f2c7f42ee Обновить 'README.md' 5 maanden geleden
WpfApp1.sln 701db76946 lab 5 maanden geleden

README.md

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

xml

<StackPanel>
        <TextBox 
            x:Name="myTextBox" 
            Height="30" />
        <TextBlock 
            x:Name="myTextBlock" 
            Text="{Binding 
            ElementName=myTextBox,
            Path=Text}"
            Height="30" />
    </StackPanel>

csharp

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow() {

        InitializeComponent();

        Binding binding = new Binding();

        // ýëåìåíò-èñòî÷íèê
        binding.ElementName = "myTextBox";

        // ñâîéñòâî ýëåìåíòà-èñòî÷íèêà
        binding.Path = new PropertyPath("Text");

        // óñòàíîâêà ïðèâÿçêè äëÿ ýëåìåíòà-ïðèåìíèêà
        myTextBlock.SetBinding(TextBlock.TextProperty, binding);
        }
     }
}

Свойство Source

xml

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

csharp

namespace WpfApp1
{
    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; }
    }

}

Свойство TargetNullValue

xml

<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

xml

<Grid Background="Aquamarine">
        <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}" 
    x:DataType="local:Phone"
>
    <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}" />
    </Grid>
</Window>   

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

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

xml

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