< >WPF bindings in a switch

Source: Internet
Author: User

Binding is a very important part of the development process for WPF applications.

In the actual development process of the different types of binding to achieve the same effect but the fact that there is a big difference.

Here will be the actual problems encountered in the summary record and understanding.

1. Source = {Binding} and Source = {Binding Relativesource={relativesource Self},path=datacontext} have the same effect

Understanding: {Binding} does not set the explicit binding of source, so the binding goes from this control class to starting from the hierarchy of the visual tree to find the value of the DataContext property that is not empty.

{Binding Relativesource={relativesource Self},path=datacontext} RelativeSource Self means that the bound source is the control itself, so the binding A datacontext that binds its own control.

Effect:

<stackpanel datacontext= "abc" > <label content= "{Binding}" ></Label> <label content= "{B Inding relativesource={relativesource Self},path=datacontext} "></Label> </StackPanel>

<stackpanel datacontext= "abc" > <label content= "{Binding}" ></Label> <label DataContext = "Def" content= "{Binding relativesource={relativesource Self},path=datacontext}" ></Label> </stackpanel >

2. Change the properties of a style control in the template in the trigger of the template

    <style targettype= "{x:type button}" >         < Setter property= "Template" >             < Setter.value>                 < ControlTemplate targettype= "{x:type button}" >                      <Border>                          < Label x:name= "Part_label" content= "{TemplateBinding ContentA}"/>                      </border>

<ControlTemplate.Triggers> <trigger property= "IsChecked" value= "True" >Note: <setter targetname= "Part_label" property= "Content" value= "{Binding path=contentb, relativ Esource={relativesource templatedparent}} "/></Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

Of course Note: This sentence changed to <setter targetname= "Part_label" property= "Content" value= "{Binding path=contentb, relativesource={relativesource ancestortype={x:Type Button }}} "> effect.

Let's take a look at elementname,source,relativesource three ways to bind

The 1.ElementName is, by definition, binding according to the name of the UI element:

Example:

<window x:name= "MainWindow" >

<Grid> <button background= "{Binding elementname=MainWindow, Path=background}"/> &L T;/grid>

</Window>

The effect is equal to

<Window>

<Grid> <button background= "{Binding relativesource={relativesource mode=findancestor, Ancestortype ={x:type window},path=background} "/> </Grid>

</Window>

Difference:

The ElementName property is used to refer to the name of a UI object whose scope is within the same XAML file and cannot reference one of the UI element names of another XAML file.

The 2.Source property is used to specify a reference to the object binding path. The feature is that the source property is typically used when binding the set object when it is known.

<window x:name= "MainWindow" >

<Grid> <button background= "{Binding source={staticresource ButtonStyle}}"/> </grid& Gt

</Window>

3.RelativeSource

You need to use RelativeSource when you are unsure of the binding source, but you know the relative relationship of the binding object, which is the biggest difference between RelativeSource and elementname and source.

Three typical uses of RelativeSource:

One property of the/1.ui element is bound to another property on its own

<label Background = {Binding Path=forgroud, Relativesource={relativesource self}}/>

A property of the/2.ui element is bound to the property of a parent element

<Grid>

<label Background = {Binding Path=background, Relativesource={relativesource ancestortype={x:type grid}}}/>

</Grid>

The properties of elements in/3.template are bound on the properties of the template consumer element

{Binding path=pathtoproperty, Relativesource={relativesource templatedparent}}

Example:

<style targettype= "{x:type Local:numericupdown}" >   <setter property= "HorizontalAlignment" Value= " Center "/>   <setter property=" verticalalignment "value=" center "/>   <setter property=" Template " >     <Setter.Value>       <controltemplate targettype= "{x:type Local:numericupdown} ">         <grid margin=" 3 ">            <Grid.RowDefinitions>              <RowDefinition/>              <RowDefinition/>           </Grid.RowDefinitions>           <Grid.ColumnDefinitions>              <ColumnDefinition/>             <ColumnDefinition/>           </Grid.ColumnDefinitions>           <border borderthickness = "1" borderbrush= "Gray"                    margin= "2" grid.rowspan= "2"                    verticalalignment= "Center" horizontalalignment= "Stretch" >

<textblock text= "{Binding relativesource={relativesource templatedparent}, Path=value}" width= "6     0 "textalignment=" right "padding=" 5 "/> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

Using TemplateBinding to bind a template to a property between the original object

{TemplateBinding Path=pathtoproperty}

Example:

  <controltemplate targettype= "{x:type button}"   x:key= "Buttontemp" >                                 <border borderthickness= "3" background= "{TemplateBinding Foreground}" >                                <textblock foreground= "{TemplateBinding Background}"/>                              </Border>                        </controltemplate> 

< go to >WPF bindings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.