Binding - vjj root page

Transkript

Binding - vjj root page
binding
28.1.16
vjj
1
diferenciální diagnoza
•
Win32 API
include <windows.h> ;
•
Windows.Forms
using System.Windows.Forms ;
•
Windows.Controls
using System.Windows.Controls ;
•
XAML
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
•
Windows.UI
using Windows.UI.XAML.Controls ;
28.1.2016
vjj
2
Binding
•
data binding is about tying together arbitrary .NET
objects
•
sample 0
28.1.16
vjj
3
old style binding
<TextBlock x:Name="currentFolder"
DockPanel.Dock="Top"
Background="AliceBlue"
FontSize="16" />
void treeView_SelectedItemChanged(
object sender,
RoutedPropertyChangedEventArgs<object> e)
{ currentFolder.Text =
(treeView.SelectedItem
as TreeViewItem).Header.ToString();
Refresh();
}
28.1.16
vjj
4
Binding in C#
Binding binding = new Binding( );
// Set source object
binding.Source = treeView;
// Set source property
binding.Path = new PropertyPath(
"SelectedItem.Header" );
// Attach to target property
currentFolder.SetBinding(
TextBlock.TextPoperty, binding );
28.1.16
vjj
5
Binding in XAML
<TextBlock x:Name="currentFolder"
Text="{Binding
ElementName=treeView,
Path=SelectedItem.Header}" />
28.1.16
vjj
6
dependency properties
•
The example with the TreeView and the Label works
because both the target and source properties are
dependency properties.
•
Dependency properties have plumbing for change
notification built in.
28.1.16
vjj
7
Binding
•
to explicitly set the source element of the Binding
•
•
•
•
ElementName – named elements in the application
Source
RelativeSource
Path – to the property inside of the source object
•
•
•
tečková notace
indexování pomocí hranatých závorek
attached property musí být uzavřena v kulatých závorkách
Path=(Parent.Children)[0].(DockPanel.Dock)
28.1.16
vjj
8
XAML element "Binding"
<Binding
ElementName=...
Path=... />
<Binding
Source=...
Path=... />
<Binding
RelativeSource=...
Path=... />
28.1.16
vjj
9
formát 1: XAML element
• základní formát: Property Element Syntax
<Button Content="Click" >
<Button.Background>
<Binding ... />
</Button.Background>
</Button>
28.1.16
vjj
10
formát 1: XAML element
<Canvas Background="LightBlue" Width="300" Height="150">
<Button Height="50" FontSize="24" Margin="10" >
<Button.Width>
<Binding ElementName="Slider1"
Path="Value"/>
</Button.Width>
<Button.Content>
<Binding ElementName="Slider1"
Path="Value"/>
</Button.Content>
<Canvas.Left>
<Binding ElementName="Slider1"
Path="Value"/>
</Canvas.Left>
</Button>
<Slider Name="Slider1" Width="250" Margin="10,100"
Minimum="50" Maximum="100" Value="70" />
</Canvas>
28.1.16
vjj
11
formát 2: inline
<Canvas Background="LightBlue" Width="300" Height="150">
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding ElementName=Slider1, Path=Value}"
Content="{Binding ElementName=Slider1, Path=Value}"
Canvas.Left="{Binding ElementName=Slider1, Path=Value}" />
<Slider Name="Slider1" Width="250" Margin="20,100"
Minimum="50" Maximum="200" Value="100" />
</Canvas>
28.1.16
vjj
12
binding Button to ComboBox
<DockPanel Width="400" Height="300" Background="LightBlue"
LastChildFill="False" >
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<ComboBox Name="myComboBox" Margin="50,10"
FontSize="20" SelectedIndex="0">
<ComboBoxItem>Blue</ComboBoxItem>
<ComboBoxItem>Green</ComboBoxItem>
<ComboBoxItem>Red</ComboBoxItem>
</ComboBox>
</Border>
<Border Background="DarkOrange" Margin="10" DockPanel.Dock ="Bottom" >
<Button Content="Select Background Color" FontSize="24" Margin="10"
Background="{Binding ElementName=myComboBox,
Path=SelectedItem.Content}" />
</Border>
</DockPanel>
28.1.16
vjj
13
Binding VisualBrush to TextBox
28.1.16
vjj
14
Binding VisualBrush to TextBox
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="500" Height="200" Background="DarkGreen">
<StackPanel Margin="40">
<TextBox x:Name="textBox" FontSize="30" Margin="0,0,20,0"/>
<Rectangle Height="{Binding ElementName=textBox, Path=ActualHeight}"
Width="{Binding ElementName=textBox, Path=ActualWidth}">
<Rectangle.Fill>
<VisualBrush Visual="{Binding ElementName=textBox}"/>
</Rectangle.Fill>
<Rectangle.LayoutTransform>
<TransformGroup>
<ScaleTransform ScaleY="-0.75"/>
<SkewTransform AngleX="30" />
</TransformGroup>
</Rectangle.LayoutTransform>
<Rectangle.OpacityMask>
<LinearGradientBrush EndPoint="0,1">
<GradientStop Offset="0" Color="Transparent"/>
<GradientStop Offset="1" Color="#77000000"/>
</LinearGradientBrush>
</Rectangle.OpacityMask>
</Rectangle>
</StackPanel>
</Page>
28.1.16
vjj
15
Path
binding to attached property
28.1.16
vjj
16
static path
<DockPanel Width="400" Height="300" Background="LightBlue"
LastChildFill="False" >
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding ElementName=Border1,
Path=(DockPanel.Dock)}" >
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding ElementName=Slider1, Path=Value}"
Content="{Binding ElementName=Slider1, Path=Value}" />
</Border>
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100"
SmallChange="1" IsSnapToTickEnabled="True"
/>
</Border>
</DockPanel>
28.1.16
vjj
17
28.1.16
vjj
18
wrong path
<DockPanel Width="400" Height="300" Background="LightBlue"
Name="DockPanel1" LastChildFill="False" >
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding ElementName=DockPanel1,
Path=Border1.(DockPanel.Dock)}" >
<!-- Border1 není property DockPanelu -->
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding ElementName=Slider1, Path=Value}"
Content="{Binding ElementName=Slider1, Path=Value}" />
</Border>
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100" />
</Border>
</DockPanel>
28.1.16
vjj
19
dynamic path - error
<DockPanel Width="400" Height="300" Background="LightBlue"
Name="DockPanel1" LastChildFill="False" >
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding ElementName=DockPanel1,
Path=Children[1].(DockPanel.Dock)}" >
<!-- DockPanel1 nemá zatím žádné Children[1] -->
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding ElementName=Slider1, Path=Value}"
Content="{Binding ElementName=Slider1, Path=Value}" />
</Border>
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100" />
</Border>
</DockPanel>
28.1.16
vjj
20
dynamic path
<DockPanel Width="400" Height="300" Background="LightBlue"
Name="DockPanel1" LastChildFill="False" >
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100" />
</Border>
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding ElementName=DockPanel1,
Path=Children[0].(DockPanel.Dock)}" >
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding ElementName=Slider1, Path=Value}"
Content="{Binding ElementName=Slider1, Path=Value}" />
</Border>
</DockPanel>
28.1.16
vjj
21
28.1.16
vjj
22
Path experiments
<DockPanel Width="400" Height="300" Background="LightBlue"
Name="DockPanel1" LastChildFill="False" >
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding ElementName=Slider1,
Path=Parent.(DockPanel.Dock)}" >
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding ElementName=Slider1, Path=Value}"
Content="{Binding ElementName=Slider1, Path=Value}" />
</Border>
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100" />
</Border>
</DockPanel>
28.1.16
vjj
23
Path experiments
<DockPanel Width="400" Height="300" Background="LightBlue"
Name="DockPanel1" LastChildFill="False" >
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding ElementName=Button1,
Path=Parent.Parent.Children[1].(DockPanel.Dock)}" >
<Button Height="50" FontSize="24" Margin="10" Name="Button1"
Width="{Binding ElementName=Slider1, Path=Value}"
Content="{Binding ElementName=Slider1, Path=Value}" />
</Border>
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100" />
</Border>
</DockPanel>
28.1.16
vjj
24
formát 3: DataContext
28.1.16
vjj
25
verze 1
<Canvas>
<Canvas.DataContext>
<Binding ElementName=... />
</Canvas.DataContext>
<Button ...>
<Button.Background>
<Binding Path=property />
<Button.Background>
</Button>
28.1.16
vjj
26
verze 2
<Canvas
DataContext="{Binding
ElementName=...}" >
<Button
Background="{Binding
28.1.16
vjj
Path=property}"/>
27
verze 3
<Canvas
DataContext="{Binding
ElementName=...}" >
<Button
Background="{Binding
28.1.16
vjj
property}"/>
28
binding with DataContext
<DockPanel Width="400" Height="300" Background="LightBlue"
Name="DockPanel1" LastChildFill="False" >
<DockPanel.DataContext>
<Binding ElementName="Slider1" />
</DockPanel.DataContext>
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100" />
</Border>
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding Path=Parent.Parent.Children[0].(DockPanel.Dock)}" >
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding Path=Value}"
Content="{Binding Value}" />
</Border>
</DockPanel>
28.1.16
vjj
29
binding with DataContext
<DockPanel Width="400" Height="300" Background="LightBlue"
Name="DockPanel1" LastChildFill="False"
DataContext="{Binding ElementName=Slider1}" >
<Border Name="Border1" Background="DarkBlue" Margin="50,10"
DockPanel.Dock="Bottom">
<Slider Name="Slider1" Margin="20"
Minimum="50" Maximum="200" Value="100" />
</Border>
<Border Background="DarkOrange" Margin="10"
DockPanel.Dock ="{Binding Path=Parent.(DockPanel.Dock)}" >
<Button Height="50" FontSize="24" Margin="10"
Width="{Binding Path=Value}"
Content="{Binding Value}" />
</Border>
</DockPanel>
28.1.16
vjj
30
Source
28.1.16
vjj
31
binding to Source x:Reference
<TextBlock
x:Name="currentFolder"
DockPanel.Dock="Top"
Text="{Binding
Source={x:Reference TreeView},
Path=SelectedItem.Header}"
Background="AliceBlue"
FontSize="16" />
28.1.16
vjj
32
binding to Source
<Page.Resources>
<Button x:Key="myButton"
Content="myButton" Width="200" FontSize="32" Margin="10">
<Button.Background>
...
</Button.Background>
</Button>
</Page.Resources>
<Button Width="300"
Content="yourButton"
Background="{Binding
Source={StaticResource myButton},
Path=Background}" />
28.1.16
vjj
34
RelativeSource
28.1.16
vjj
35
RelativeSource
{BindRelativeSourceing
RelativeSource={RelativeSource Self}}
{Binding
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DesiredType}
{Binding
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DesiredType,
AncestorLevel=n}
28.1.16
vjj
36
binding to "Self"
<TextBox Margin="10"
Text="White"
Background="{Binding
RelativeSource={RelativeSource Self},
Path=Text}"
/>
28.1.16
vjj
37
Binding Converter
• To convert data during binding, you must create a
class that implements the IValueConverter
interface, which includes the Convert and
ConvertBack methods.
28.1.16
vjj
38
[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo cult
{
DateTime date = (DateTime)value;
return date.ToShortDateString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo
{
string strValue = value as string;
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return DependencyProperty.UnsetValue;
}
}
28.1.16
vjj
39

Podobné dokumenty

Windows GUI - vjj root page

Windows GUI - vjj root page jako byste to měli naučit začátečníka nebo jiného programátora, který ale o programování pro Windows skoro nic neví

Více

rocnikovy_projekt_ny..

rocnikovy_projekt_ny.. který se nacházı́ na pravé straně od šachovnice, poskytuje základnı́ informace a statistiky o jednotlivých hráčı́ch (jméno, barva, počet figurek). Nad panelem hráčů je zobrazen počet...

Více

Převod Windows Forms do Windows Presentation

Převod Windows Forms do Windows Presentation The most extensive part of whole Bachelor’s Thesis is dedicated to description of application for converting Windows Forms into Windows Presentation Foundation. Inside the description of applicatio...

Více

Hlavní dokument [] - Vysoké učení technické v Brně

Hlavní dokument [] - Vysoké učení technické v Brně Je vhodné si uvědomit, jaký je rozdíl mezi Unreal Engine 3 (UE3) a Unreal Development Kit (UDK). Unreal Development Kit je nástroj sloužící pro nekomerční účely k vývoji her a vizualizaci. Nelze př...

Více

projekt FlashPoM-1 - Západočeská univerzita

projekt FlashPoM-1 - Západočeská univerzita This work describes one part of this project, which is to create a low-complexity software tool for chip configuration by combination of a set of predefined elements and modules. These will include...

Více

Nové prvky v C# 3.0

Nové prvky v C# 3.0 Navíc jak víme, je snadné původní třídu podědit a novou metodu přidat do potomka, tak k čemu je tento nástroj? Jeho využitelnost máme ocenit při práci s třídami, které nechceme nebo nemůžeme dědit.

Více

Internals - vjj root page

Internals - vjj root page Target instruction — Reads a 64-bit canonical address from IA32_LSTAR. IA32_LSTAR model specific register points to "KiSystemCall64" NT kernel

Více