Archive

Archives pour 10/2016

All Binding expression

Show all binding possibility i have seen

Simple binding Description
{Binding}
{Binding .}
 Binding to DataContext
{Binding Name}
{Binding Path=Name}
 Binding to « Name » property of object contained in DataContext
{Binding Selected.Name}
{Binding Path=Selected.Name}
 Binding to Selected and inside selected to « Name » property of object contained in DataContext
Binding to xaml object Description
{Binding Width,ElementName=alpha}
{Binding Path=Width,ElementName=alpha}
Binding to another control in xaml (x:Name must be specified)
{Binding DataContext.IsDirty,ElementName=alpha} Binding to DataContext contained in Control named alpha and set IsDirty
Binding to static Description
{Binding Source={x:Static s:DateTime.Now}}
{x:Static s:DateTime.Now}
 Binding on Static DateTime with
{Binding Source={StaticResource ElementKey}}
{StaticResource ElementKey}
 Binding on StaticResource named « ElementKey »
{Binding Source={DynamicResource ElementKey}}
{DynamicResource ElementKey}
 Binding on DynamicResource named « ElementKey »
{x:Static diisyetem:Node.Nodes}  Binding on static Nodes
Binding to Singleton Description
{Binding ShowGrid, Source={x:Static local:DesignBackgroundDatas.Current }}  Binding on current static local:DesignBackgroundDatas (singleton current) and binding to ShwoGrid
binding self to compoment Description
Width=“{Binding ActualHeight, RelativeSource={RelativeSource Self}}“ Binding to itself for setting Width from ActualHeight
binding to template (for CustomControl) Description
{TemplateBinding ToolPlay} Simple binding to property of CustomControl
{Binding PlayCommand, RelativeSource={RelativeSource TemplatedParent}} Same as previous but can speciy if necessary Mode, Converter etc …
or when templatebinding don’t work (sometime possible)
binding parent élément (hierarchical find) Description
{Binding Foreground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}} Binding on Foreground of parent Button (first find)
{Binding Path=Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=2,AncestorType=Grid} Binding on tag of second parent grid (on up from compoment first grid parent finded is ignored and second is set)
Binding to attached property Description
{Binding Path=(local:Attached.Test),
RelativeSource={RelativeSource Self}}
Specified with () Binding to AttachedTest property of current control (self)
attached property are set only for Control (you must specify it)
{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContentControl},
Path=(AutomationProperties.AutomationId)} »
Same as previous but with ancestor for binding to attached property AutomationProperties.AutomationId of contentcontrol
Samples
<TextBlock x:Name=“t1“ local:TestClass.Test=“abc“ Grid.Column=“3“/>
<TextBlock Text=“{Binding Path=(local:TestClass.Test), ElementName=t1}“/>
<TextBlock Text=“{Binding (Grid.Column), ElementName=t1}“/>
Binding to array/Collection (specific) Description
{Binding Food[2]} Binding of element 2 of array Food
Content=“{Binding Path=/}“ / char set the binding of current item in collection (in datacontext)
Content=“{Binding Path=/IsDirty}“ Same as previous with set of IsDirty property of current item in collection
MultiBinding Description
<MultiBinding StringFormat=“{}{0} x {1}“>
<Binding Path=“Width“ />
<Binding Path=“Height“ />
</MultiBinding>
Binding for create a text without converter
<MultiBinding Converter=“{StaticResource MultiValueConverter}“>
<Binding Path=“FirstName“/>
<Binding Path=“Surname“ />
</MultiBinding>
with
<local:NameMultiValueConverter x:Key=“NameMultiValueConverter“ />
Multibinding using IMultiValueConverter
public class NameMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture) {
return String.Format(“{0} {1}“, values[0], values[1]);
}
}
Categories: wpf Tags: ,