|
@@ -1,418 +1,521 @@
|
|
-# Привязка (Binding). Интерфейс INotifyPropertyChanged. Форматирование значений привязки и конвертеры значений.
|
|
|
|
-## Введение в привязку данных
|
|
|
|
|
|
+# Элементы управления
|
|
|
|
+## Обзор элементов управления и их свойств
|
|
|
|
+### Visibility
|
|
```
|
|
```
|
|
-<Window
|
|
|
|
- x:Class="BindingApp.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:BindingApp"
|
|
|
|
- mc:Ignorable="d"
|
|
|
|
- Title="MainWindow"
|
|
|
|
- Height="250"
|
|
|
|
- Width="300"
|
|
|
|
->
|
|
|
|
- <StackPanel>
|
|
|
|
- <TextBox
|
|
|
|
- x:Name="myTextBox"
|
|
|
|
- Height="30" />
|
|
|
|
- <TextBlock
|
|
|
|
- x:Name="myTextBlock"
|
|
|
|
- Text="{Binding
|
|
|
|
- ElementName=myTextBox,
|
|
|
|
- Path=Text}"
|
|
|
|
- Height="30" />
|
|
|
|
|
|
+<Grid>
|
|
|
|
+ <Grid.ColumnDefinitions>
|
|
|
|
+ <ColumnDefinition Width="*" />
|
|
|
|
+ <ColumnDefinition Width="*" />
|
|
|
|
+ </Grid.ColumnDefinitions>
|
|
|
|
+ <StackPanel
|
|
|
|
+ Grid.Column="0"
|
|
|
|
+ Background="Lavender">
|
|
|
|
+ <Button
|
|
|
|
+ Visibility="Collapsed"
|
|
|
|
+ Content="Панель Collapsed" />
|
|
|
|
+ <Button
|
|
|
|
+ Height="20"
|
|
|
|
+ Content="Visible Button" />
|
|
</StackPanel>
|
|
</StackPanel>
|
|
-</Window>
|
|
|
|
|
|
+ <StackPanel
|
|
|
|
+ Grid.Column="1"
|
|
|
|
+ Background="LightGreen">
|
|
|
|
+ <Button
|
|
|
|
+ Visibility="Hidden"
|
|
|
|
+ Content="Панель Hidden" />
|
|
|
|
+ <Button
|
|
|
|
+ Height="20"
|
|
|
|
+ Content="Visible Button" />
|
|
|
|
+ </StackPanel>
|
|
|
|
+</Grid>
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20010935.png)
|
|
|
|
-## Работа с привязкой в C#
|
|
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20035522.png)
|
|
|
|
+### Свойства настройки шрифтов
|
|
```
|
|
```
|
|
-public MainWindow()
|
|
|
|
-{
|
|
|
|
- InitializeComponent();
|
|
|
|
-
|
|
|
|
- Binding binding = new Binding();
|
|
|
|
-
|
|
|
|
- // элемент-источник
|
|
|
|
- binding.ElementName = "myTextBox";
|
|
|
|
-
|
|
|
|
- // свойство элемента-источника
|
|
|
|
- binding.Path = new PropertyPath("Text");
|
|
|
|
-
|
|
|
|
- // установка привязки для элемента-приемника
|
|
|
|
- myTextBlock.SetBinding(TextBlock.TextProperty, binding);
|
|
|
|
-}
|
|
|
|
|
|
+<Button
|
|
|
|
+ Content="Hello World!"
|
|
|
|
+ FontFamily="Verdana"
|
|
|
|
+ FontSize="13"
|
|
|
|
+ FontStretch="Expanded" />
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20012626.png)
|
|
|
|
-## Режимы привязки
|
|
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20040241.png)
|
|
|
|
+### FlowDirection
|
|
```
|
|
```
|
|
<StackPanel>
|
|
<StackPanel>
|
|
- <TextBox
|
|
|
|
- x:Name="textBox1"
|
|
|
|
- Height="30" />
|
|
|
|
- <TextBox
|
|
|
|
- x:Name="textBox2"
|
|
|
|
- Height="30"
|
|
|
|
- Text="{Binding
|
|
|
|
- ElementName=textBox1,
|
|
|
|
- Path=Text,
|
|
|
|
- Mode=TwoWay}" />
|
|
|
|
|
|
+ <TextBlock FlowDirection="RightToLeft">
|
|
|
|
+ RightToLeft
|
|
|
|
+ </TextBlock>
|
|
|
|
+ <TextBlock FlowDirection="LeftToRight">
|
|
|
|
+ LeftToRight
|
|
|
|
+ </TextBlock>
|
|
</StackPanel>
|
|
</StackPanel>
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20011715.png)
|
|
|
|
-## Свойство Source
|
|
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20040054.png)
|
|
|
|
+### Цвета фона и шрифта
|
|
```
|
|
```
|
|
-class Phone
|
|
|
|
-{
|
|
|
|
- public string Title { get; set; }
|
|
|
|
- public string Company { get; set; }
|
|
|
|
- public int Price { get; set; }
|
|
|
|
-}
|
|
|
|
|
|
+<Button
|
|
|
|
+ Width="60"
|
|
|
|
+ Height="30"
|
|
|
|
+ Background="LightGray"
|
|
|
|
+ Foreground="DarkRed"
|
|
|
|
+ Content="Цвет" />
|
|
```
|
|
```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20040406.png)
|
|
|
|
+## Элементы управления содержимым
|
|
```
|
|
```
|
|
<Window
|
|
<Window
|
|
- x:Class="BindingApp.MainWindow"
|
|
|
|
|
|
+ x:Class="ControlsApp.MainWindow"
|
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
- xmlns:local="clr-namespace:BindingApp"
|
|
|
|
|
|
+ xmlns:local="clr-namespace:ControlsApp"
|
|
mc:Ignorable="d"
|
|
mc:Ignorable="d"
|
|
- Title="MainWindow"
|
|
|
|
- Height="150"
|
|
|
|
|
|
+ Title="Элементы управления"
|
|
|
|
+ Height="250"
|
|
Width="300"
|
|
Width="300"
|
|
>
|
|
>
|
|
- <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>
|
|
|
|
|
|
+ <StackPanel>
|
|
|
|
+ <Button
|
|
|
|
+ x:Name="button1" />
|
|
|
|
+ </StackPanel>
|
|
</Window>
|
|
</Window>
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20013928.png)
|
|
|
|
-## Свойство TargetNullValue
|
|
|
|
```
|
|
```
|
|
-<Window.Resources>
|
|
|
|
- <local:Phone
|
|
|
|
- x:Key="nexusPhone"
|
|
|
|
- Company="Google"
|
|
|
|
- Price="25000" />
|
|
|
|
-</Window.Resources>
|
|
|
|
|
|
+using System;
|
|
|
|
+using System.Windows;
|
|
|
|
+
|
|
|
|
+namespace ControlsApp
|
|
|
|
+{
|
|
|
|
+ public partial class MainWindow : Window
|
|
|
|
+ {
|
|
|
|
+ public MainWindow()
|
|
|
|
+ {
|
|
|
|
+ InitializeComponent();
|
|
|
|
+ double d = 5.6;
|
|
|
|
+ button1.Content = d;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20041022.png)
|
|
|
|
+## Позиционирование контента
|
|
|
|
+```
|
|
<StackPanel>
|
|
<StackPanel>
|
|
- <TextBlock
|
|
|
|
- x:Name="titleTextBlock"
|
|
|
|
- Text="{Binding
|
|
|
|
- Source={StaticResource nexusPhone},
|
|
|
|
- Path=Title,
|
|
|
|
- TargetNullValue=Текст по умолчанию}" />
|
|
|
|
|
|
+ <Button
|
|
|
|
+ Margin="5"
|
|
|
|
+ HorizontalContentAlignment="Left"
|
|
|
|
+ Content="Left"
|
|
|
|
+ Height="90"
|
|
|
|
+ Width="500" />
|
|
|
|
+ <Button
|
|
|
|
+ Margin="5"
|
|
|
|
+ HorizontalContentAlignment="Right"
|
|
|
|
+ Content="Right"
|
|
|
|
+ Height="90"
|
|
|
|
+ Width="500" />
|
|
|
|
+ <Button
|
|
|
|
+ Margin="5"
|
|
|
|
+ HorizontalContentAlignment="Center"
|
|
|
|
+ Content="Center"
|
|
|
|
+ Height="90"
|
|
|
|
+ Width="500" />
|
|
</StackPanel>
|
|
</StackPanel>
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20013928.png)
|
|
|
|
-## Свойство RelativeSource
|
|
|
|
-```
|
|
|
|
-<TextBox Text="{Binding
|
|
|
|
- RelativeSource={RelativeSource Mode=Self},
|
|
|
|
- Path=Background,
|
|
|
|
- Mode=TwoWay,
|
|
|
|
- UpdateSourceTrigger=PropertyChanged}" />
|
|
|
|
-```
|
|
|
|
-![](./img/Снимок%20экрана%202024-04-23%20014452.png)
|
|
|
|
-```
|
|
|
|
-<Grid Background="Black">
|
|
|
|
- <TextBlock
|
|
|
|
- Foreground="White"
|
|
|
|
- Text="{Binding
|
|
|
|
- RelativeSource={RelativeSource
|
|
|
|
- Mode=FindAncestor,
|
|
|
|
- AncestorType={x:Type Grid}},
|
|
|
|
- Path=Background}" />
|
|
|
|
-</Grid>
|
|
|
|
-```
|
|
|
|
-![](./img/Снимок%20экрана%202024-04-23%20014616.png)
|
|
|
|
-## Свойство DataContext
|
|
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20041447.png)
|
|
|
|
+### Padding
|
|
```
|
|
```
|
|
-<Window
|
|
|
|
- x:Class="BindingApp.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:BindingApp"
|
|
|
|
- mc:Ignorable="d"
|
|
|
|
- Title="MainWindow"
|
|
|
|
- Height="150"
|
|
|
|
- Width="300">
|
|
|
|
- <Window.Resources>
|
|
|
|
- <local:Phone
|
|
|
|
- x:Key="nexusPhone"
|
|
|
|
- Title="Nexus X5"
|
|
|
|
- Company="Google"
|
|
|
|
- Price="25000" />
|
|
|
|
- </Window.Resources>
|
|
|
|
- <Grid
|
|
|
|
- Background="Black"
|
|
|
|
- DataContext="{StaticResource nexusPhone}"
|
|
|
|
- TextBlock.Foreground="White">
|
|
|
|
- <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>
|
|
|
|
|
|
+<StackPanel>
|
|
|
|
+ <Button
|
|
|
|
+ x:Name="button1"
|
|
|
|
+ Padding="50 30 0 40"
|
|
|
|
+ HorizontalContentAlignment="Left">
|
|
|
|
+ Hello World
|
|
|
|
+ </Button>
|
|
|
|
+ <Button
|
|
|
|
+ x:Name="button2"
|
|
|
|
+ Padding="60 20 0 30"
|
|
|
|
+ HorizontalContentAlignment="Center">
|
|
|
|
+ Hello World
|
|
|
|
+ </Button>
|
|
|
|
+</StackPanel>
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20013928.png)
|
|
|
|
-## Интерфейс INotifyPropertyChanged.
|
|
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20041630.png)
|
|
|
|
+## Кнопки
|
|
```
|
|
```
|
|
<Window
|
|
<Window
|
|
- x:Class="BindingApp.MainWindow"
|
|
|
|
|
|
+ x:Class="ControlsApp.MainWindow"
|
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
- xmlns:local="clr-namespace:BindingApp"
|
|
|
|
|
|
+ xmlns:local="clr-namespace:ControlsApp"
|
|
mc:Ignorable="d"
|
|
mc:Ignorable="d"
|
|
- Title="MainWindow"
|
|
|
|
- Height="150"
|
|
|
|
|
|
+ Title="Элементы управления"
|
|
|
|
+ Height="250"
|
|
Width="300"
|
|
Width="300"
|
|
>
|
|
>
|
|
- <Window.Resources>
|
|
|
|
- <local:Phone
|
|
|
|
- x:Key="nexusPhone"
|
|
|
|
- Title="Nexus X5"
|
|
|
|
- Company="Google"
|
|
|
|
- Price="25000" />
|
|
|
|
- </Window.Resources>
|
|
|
|
- <Grid
|
|
|
|
- Background="Black"
|
|
|
|
- DataContext="{StaticResource nexusPhone}"
|
|
|
|
- TextBlock.Foreground="White">
|
|
|
|
- <Grid.ColumnDefinitions>
|
|
|
|
- <ColumnDefinition />
|
|
|
|
- <ColumnDefinition />
|
|
|
|
- <ColumnDefinition />
|
|
|
|
- </Grid.ColumnDefinitions>
|
|
|
|
- <Grid.RowDefinitions>
|
|
|
|
- <RowDefinition />
|
|
|
|
- <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" />
|
|
|
|
-
|
|
|
|
|
|
+ <StackPanel>
|
|
<Button
|
|
<Button
|
|
- Foreground="White"
|
|
|
|
- Content="Изменить"
|
|
|
|
- Click="Button_Click"
|
|
|
|
- Background="Black"
|
|
|
|
- BorderBrush="Silver"
|
|
|
|
- Grid.Column="2"
|
|
|
|
- Grid.Row="2" />
|
|
|
|
- </Grid>
|
|
|
|
|
|
+ x:Name="acceptButton"
|
|
|
|
+ Content="ОК"
|
|
|
|
+ IsDefault="True"
|
|
|
|
+ Click="acceptButton_Click" />
|
|
|
|
+ <Button
|
|
|
|
+ x:Name="escButton"
|
|
|
|
+ Content="Выход"
|
|
|
|
+ IsCancel="True"
|
|
|
|
+ Click="escButton_Click" />
|
|
|
|
+ </StackPanel>
|
|
</Window>
|
|
</Window>
|
|
```
|
|
```
|
|
```
|
|
```
|
|
-using System.ComponentModel;
|
|
|
|
-using System.Runtime.CompilerServices;
|
|
|
|
|
|
+using System.Windows;
|
|
|
|
|
|
-class Phone : INotifyPropertyChanged
|
|
|
|
|
|
+namespace ControlsApp
|
|
{
|
|
{
|
|
- private string title;
|
|
|
|
- private string company;
|
|
|
|
- private int price;
|
|
|
|
-
|
|
|
|
- public string Title
|
|
|
|
|
|
+ public partial class MainWindow : Window
|
|
{
|
|
{
|
|
- get { return title; }
|
|
|
|
- set
|
|
|
|
|
|
+ public MainWindow()
|
|
{
|
|
{
|
|
- title = value;
|
|
|
|
- OnPropertyChanged("Title");
|
|
|
|
|
|
+ InitializeComponent();
|
|
}
|
|
}
|
|
- }
|
|
|
|
- public string Company
|
|
|
|
- {
|
|
|
|
- get { return company; }
|
|
|
|
- set
|
|
|
|
|
|
+
|
|
|
|
+ private void acceptButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
{
|
|
- company = value;
|
|
|
|
- OnPropertyChanged("Company");
|
|
|
|
|
|
+ MessageBox.Show("Действие выполнено");
|
|
}
|
|
}
|
|
- }
|
|
|
|
- public int Price
|
|
|
|
- {
|
|
|
|
- get { return price; }
|
|
|
|
- set
|
|
|
|
|
|
+
|
|
|
|
+ private void escButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
{
|
|
- price = value;
|
|
|
|
- OnPropertyChanged("Price");
|
|
|
|
|
|
+ this.Close(); // закрытие окна
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20041942.png)
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20042016.png)
|
|
|
|
+### CheckBox
|
|
|
|
+```
|
|
|
|
+<StackPanel x:Name="stackPanel">
|
|
|
|
+ <CheckBox
|
|
|
|
+ x:Name="checkBox1"
|
|
|
|
+ IsThreeState="True"
|
|
|
|
+ IsChecked="False"
|
|
|
|
+ Height="20"
|
|
|
|
+ Content="Неотмечено" />
|
|
|
|
+ <CheckBox
|
|
|
|
+ x:Name="checkBox2"
|
|
|
|
+ IsThreeState="True"
|
|
|
|
+ IsChecked="True"
|
|
|
|
+ Height="20"
|
|
|
|
+ Content="Отмечено" />
|
|
|
|
+ <CheckBox
|
|
|
|
+ x:Name="checkBox3"
|
|
|
|
+ IsThreeState="True"
|
|
|
|
+ IsChecked="{x:Null}"
|
|
|
|
+ Height="20"
|
|
|
|
+ Content="Неопределено"/>
|
|
|
|
+</StackPanel>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20042337.png)
|
|
|
|
+```
|
|
|
|
+<CheckBox
|
|
|
|
+ x:Name="checkBox"
|
|
|
|
+ IsChecked="False"
|
|
|
|
+ Height="20"
|
|
|
|
+ Content="Флажок"
|
|
|
|
+ IsThreeState="True"
|
|
|
|
+ Unchecked="checkBox_Unchecked"
|
|
|
|
+ Indeterminate="checkBox_Indeterminate"
|
|
|
|
+ Checked="checkBox_Checked" />
|
|
|
|
+```
|
|
|
|
+```
|
|
|
|
+private void checkBox_Checked(object sender, RoutedEventArgs e)
|
|
|
|
+{
|
|
|
|
+ MessageBox.Show(checkBox.Content.ToString() + " отмечен");
|
|
|
|
+}
|
|
|
|
|
|
- public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
- public void OnPropertyChanged([CallerMemberName]string prop = "")
|
|
|
|
- {
|
|
|
|
- if (PropertyChanged != null)
|
|
|
|
- PropertyChanged(this, new PropertyChangedEventArgs(prop));
|
|
|
|
- }
|
|
|
|
|
|
+private void checkBox_Unchecked(object sender, RoutedEventArgs e)
|
|
|
|
+{
|
|
|
|
+ MessageBox.Show(checkBox.Content.ToString() + " не отмечен");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+private void checkBox_Indeterminate(object sender, RoutedEventArgs e)
|
|
|
|
+{
|
|
|
|
+ MessageBox.Show(checkBox.Content.ToString() + " в неопределенном состоянии");
|
|
|
|
+}
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20042559.png)
|
|
|
|
+### RadioButton
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20032736.png)
|
|
|
|
-## Форматирование значений привязки и конвертеры значений
|
|
|
|
|
|
+<StackPanel x:Name="stackPanel">
|
|
|
|
+ <RadioButton
|
|
|
|
+ GroupName="Languages"
|
|
|
|
+ Content="C#"
|
|
|
|
+ IsChecked="True" />
|
|
|
|
+ <RadioButton
|
|
|
|
+ GroupName="Languages"
|
|
|
|
+ Content="VB.NET" />
|
|
|
|
+ <RadioButton
|
|
|
|
+ GroupName="Languages"
|
|
|
|
+ Content="C++" />
|
|
|
|
+ <RadioButton
|
|
|
|
+ GroupName="Technologies"
|
|
|
|
+ Content="WPF"
|
|
|
|
+ IsChecked="True" />
|
|
|
|
+ <RadioButton
|
|
|
|
+ GroupName="Technologies"
|
|
|
|
+ Content="WinForms" />
|
|
|
|
+ <RadioButton
|
|
|
|
+ GroupName="Technologies"
|
|
|
|
+ Content="ASP.NET" />
|
|
|
|
+</StackPanel>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20042846.png)
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20042956.png)
|
|
|
|
+## Текстовые элементы управления
|
|
|
|
+### TextBox
|
|
|
|
+```
|
|
|
|
+<TextBox
|
|
|
|
+ MaxLength="250"
|
|
|
|
+ TextChanged="TextBox_TextChanged">
|
|
|
|
+ Начальный текст
|
|
|
|
+</TextBox>
|
|
```
|
|
```
|
|
-class Phone
|
|
|
|
|
|
+```
|
|
|
|
+private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
{
|
|
- public string Title { get; set; }
|
|
|
|
- public string Company { get; set; }
|
|
|
|
- public int Price { get; set; }
|
|
|
|
|
|
+ TextBox textBox = (TextBox)sender;
|
|
|
|
+ MessageBox.Show(textBox.Text);
|
|
|
|
+}
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20043731.png)
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20043810.png)
|
|
|
|
+```
|
|
|
|
+<TextBox
|
|
|
|
+ AcceptsReturn="True"
|
|
|
|
+ Height="100"
|
|
|
|
+ VerticalScrollBarVisibility="Auto"
|
|
|
|
+ HorizontalScrollBarVisibility="Auto">
|
|
|
|
+ Начальный текст
|
|
|
|
+</TextBox>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20044027.png)
|
|
|
|
+```
|
|
|
|
+<StackPanel>
|
|
|
|
+ <TextBox
|
|
|
|
+ x:Name="textBox1"
|
|
|
|
+ Height="100" SelectionBrush="Blue" />
|
|
|
|
+ <Button
|
|
|
|
+ Content="Выделить текст"
|
|
|
|
+ Height="30"
|
|
|
|
+ Width="100"
|
|
|
|
+ Click="Button_Click"
|
|
|
|
+ Margin="10" />
|
|
|
|
+</StackPanel>
|
|
|
|
+```
|
|
|
|
+```
|
|
|
|
+private void Button_Click(object sender, RoutedEventArgs e)
|
|
|
|
+{
|
|
|
|
+ textBox1.SelectionStart = 5;
|
|
|
|
+ textBox1.SelectionLength = 10;
|
|
|
|
+ textBox1.Focus();
|
|
|
|
+ // данное выражение эквивалентно
|
|
|
|
+ //textBox1.Select(5, 10);
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20044757.png)
|
|
|
|
+### Проверка орфографии
|
|
|
|
+```
|
|
|
|
+<DockPanel>
|
|
|
|
+ <TextBox
|
|
|
|
+ SpellCheck.IsEnabled="True"
|
|
|
|
+ Language="ru-ru">
|
|
|
|
+ Привет, как дила?
|
|
|
|
+ </TextBox>
|
|
|
|
+</DockPanel>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20055505.png)
|
|
|
|
+### PasswordBox
|
|
|
|
+```
|
|
|
|
+<StackPanel>
|
|
|
|
+ <PasswordBox
|
|
|
|
+ PasswordChar="*"
|
|
|
|
+ MinHeight="30" />
|
|
|
|
+ <PasswordBox
|
|
|
|
+ MinHeight="30" />
|
|
|
|
+</StackPanel>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20045358.png)
|
|
|
|
+## Элементы управления списками
|
|
```
|
|
```
|
|
<Window
|
|
<Window
|
|
- x:Class="ValueConventerApp.MainWindow"
|
|
|
|
|
|
+ x:Class="ControlsApp.MainWindow"
|
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
- xmlns:local="clr-namespace:ValueConventerApp"
|
|
|
|
|
|
+ xmlns:local="clr-namespace:ControlsApp"
|
|
|
|
+ xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
|
mc:Ignorable="d"
|
|
mc:Ignorable="d"
|
|
- Title="MainWindow"
|
|
|
|
- Height="150"
|
|
|
|
|
|
+ Title="ListBox"
|
|
|
|
+ Height="200"
|
|
Width="300"
|
|
Width="300"
|
|
>
|
|
>
|
|
- <Window.Resources>
|
|
|
|
- <local:Phone
|
|
|
|
- x:Key="nexusPhone"
|
|
|
|
- Title="Nexus X5"
|
|
|
|
- Company="Google"
|
|
|
|
- Price="25000" />
|
|
|
|
- </Window.Resources>
|
|
|
|
<Grid>
|
|
<Grid>
|
|
- <TextBlock
|
|
|
|
- Text="{Binding
|
|
|
|
- StringFormat=Итоговая цена {0} рублей,
|
|
|
|
- Source={StaticResource nexusPhone},
|
|
|
|
- Path=Price}" />
|
|
|
|
|
|
+ <ListBox Name="list">
|
|
|
|
+ <sys:String>Lumia 950</sys:String>
|
|
|
|
+ <sys:String>iPhone 6S Plus</sys:String>
|
|
|
|
+ <sys:String>Xiaomi Mi5</sys:String>
|
|
|
|
+ <sys:String>Nexus 5X</sys:String>
|
|
|
|
+ </ListBox>
|
|
</Grid>
|
|
</Grid>
|
|
</Window>
|
|
</Window>
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20033322.png)
|
|
|
|
-## Конвертеры значений
|
|
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20045645.png)
|
|
|
|
+### ComboBox
|
|
|
|
+```
|
|
|
|
+<ComboBox
|
|
|
|
+ Name="phonesList"
|
|
|
|
+ Height="30"
|
|
|
|
+ VerticalAlignment="Top">
|
|
|
|
+ <TextBlock>LG Nexus 5X</TextBlock>
|
|
|
|
+ <TextBlock>Huawai Nexus 6P</TextBlock>
|
|
|
|
+ <TextBlock>iPhone 6S</TextBlock>
|
|
|
|
+ <TextBlock>iPhone 6S Plus</TextBlock>
|
|
|
|
+ <TextBlock>Microsoft Lumia 950</TextBlock>
|
|
|
|
+</ComboBox>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20050332.png)
|
|
|
|
+### ComboBoxItem
|
|
|
|
+```
|
|
|
|
+<ComboBox
|
|
|
|
+ Height="50"
|
|
|
|
+ Width="150"
|
|
|
|
+ VerticalAlignment="Top"
|
|
|
|
+>
|
|
|
|
+ <ComboBoxItem IsSelected="True">
|
|
|
|
+ <StackPanel Orientation="Horizontal">
|
|
|
|
+ <Image
|
|
|
|
+ Source="cats.jpg"
|
|
|
|
+ Width="60" />
|
|
|
|
+ <TextBlock>cats.jpg</TextBlock>
|
|
|
|
+ </StackPanel>
|
|
|
|
+ </ComboBoxItem>
|
|
|
|
+ <StackPanel Orientation="Horizontal">
|
|
|
|
+ <Image
|
|
|
|
+ Source="bear.jpg"
|
|
|
|
+ Width="60" />
|
|
|
|
+ <TextBlock>bear.jpg</TextBlock>
|
|
|
|
+ </StackPanel>
|
|
|
|
+ <StackPanel Orientation="Horizontal">
|
|
|
|
+ <Image
|
|
|
|
+ Source="panda.jpg"
|
|
|
|
+ Width="60" />
|
|
|
|
+ <TextBlock>panda.jpg</TextBlock>
|
|
|
|
+ </StackPanel>
|
|
|
|
+</ComboBox>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20061047.png)
|
|
|
|
+## DataGrid
|
|
```
|
|
```
|
|
-public class DateTimeToDateConverter : IValueConverter
|
|
|
|
|
|
+public class Phone
|
|
{
|
|
{
|
|
- 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;
|
|
|
|
- }
|
|
|
|
|
|
+ public string Title { get; set; }
|
|
|
|
+ public string Company { get; set; }
|
|
|
|
+ public int Price { get; set; }
|
|
}
|
|
}
|
|
```
|
|
```
|
|
```
|
|
```
|
|
-<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>
|
|
|
|
|
|
+<DataGrid
|
|
|
|
+ x:Name="phonesGrid"
|
|
|
|
+ AutoGenerateColumns="False"
|
|
|
|
+ HorizontalGridLinesBrush="DarkGray"
|
|
|
|
+ RowBackground="LightGray"
|
|
|
|
+ AlternatingRowBackground="White">
|
|
|
|
+
|
|
|
|
+ <DataGrid.Items>
|
|
|
|
+ <local:Phone
|
|
|
|
+ Title="iPhone 6S"
|
|
|
|
+ Company="Apple"
|
|
|
|
+ Price="54990" />
|
|
|
|
+ <local:Phone
|
|
|
|
+ Title="Lumia 950"
|
|
|
|
+ Company="Microsoft"
|
|
|
|
+ Price="39990" />
|
|
|
|
+ <local:Phone
|
|
|
|
+ Title="Nexus 5X"
|
|
|
|
+ Company="Google"
|
|
|
|
+ Price="29990" />
|
|
|
|
+ </DataGrid.Items>
|
|
|
|
+ <DataGrid.Columns>
|
|
|
|
+ <DataGridTextColumn
|
|
|
|
+ Header="Модель"
|
|
|
|
+ Binding="{Binding Path=Title}"
|
|
|
|
+ Width="90" />
|
|
|
|
+ <DataGridHyperlinkColumn
|
|
|
|
+ Header="Компания"
|
|
|
|
+ Binding="{Binding Path=Company}"
|
|
|
|
+ Width="80" />
|
|
|
|
+ <DataGridTextColumn
|
|
|
|
+ Header="Цена"
|
|
|
|
+ Binding="{Binding Path=Price}"
|
|
|
|
+ Width="50" />
|
|
|
|
+ </DataGrid.Columns>
|
|
|
|
+
|
|
|
|
+ <DataGrid.RowDetailsTemplate>
|
|
|
|
+ <DataTemplate>
|
|
|
|
+ <StackPanel Orientation="Horizontal">
|
|
|
|
+ <TextBlock
|
|
|
|
+ Text="{Binding Path=Price}" />
|
|
|
|
+ <TextBlock
|
|
|
|
+ Text=" рублей по скидке" />
|
|
|
|
+ </StackPanel>
|
|
|
|
+ </DataTemplate>
|
|
|
|
+ </DataGrid.RowDetailsTemplate>
|
|
|
|
+
|
|
|
|
+</DataGrid>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20053857.png)
|
|
|
|
+## Работа с изображениями. Image и InkCanvas
|
|
|
|
+### Элемент Image
|
|
|
|
+```
|
|
|
|
+ <Grid Background="Black">
|
|
|
|
+ <Grid.ColumnDefinitions>
|
|
|
|
+ <ColumnDefinition Width="2.5*" />
|
|
|
|
+ <ColumnDefinition Width="*" />
|
|
|
|
+ </Grid.ColumnDefinitions>
|
|
|
|
+ <Image Grid.Column="0" x:Name="mainImage">
|
|
|
|
+ <Image.Source>
|
|
|
|
+ <FormatConvertedBitmap Source="panda.jpg"
|
|
|
|
+ DestinationFormat="Gray32Float" />
|
|
|
|
+ </Image.Source>
|
|
|
|
+ </Image>
|
|
|
|
+ <StackPanel Grid.Column="1">
|
|
|
|
+ <Image Source="bear.jpg" />
|
|
|
|
+ <Image Source="panda.jpg" />
|
|
|
|
+ </StackPanel>
|
|
|
|
+ </Grid>
|
|
|
|
+```
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20070636.png)
|
|
|
|
+### InkCanvas
|
|
|
|
+```
|
|
|
|
+<InkCanvas>
|
|
|
|
+ <Image
|
|
|
|
+ Source="panda.jpg"
|
|
|
|
+ Width="300"
|
|
|
|
+ Height="250" />
|
|
|
|
+</InkCanvas>
|
|
```
|
|
```
|
|
-![](./img/Снимок%20экрана%202024-04-23%20031122.png)
|
|
|
|
|
|
+![](./img/Снимок%20экрана%202024-04-23%20061816.png)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|