Using bindings in styles in Silverlight

Source: Internet
Author: User

This document comes from this blog. I think it's very useful and reposted here.

Http://blogs.profitbase.com/tsenn? P = 53

One of the differences between WPF and Silverlight is that using bindings when setting properties in styles is still not supported (SL 4). If you try to do so, your application simplwill y crash.
In WPF, you can do something like this;

    style   targettype  =" {  x  :   type   listboxitem  }" >  setter   property  =" background "  value  =" {  binding   path  =mycolor}"/>   style  > 

Although not directly supported, you can achieve the same thing in Silverlight using attached properties. the workaround is to specify an attached property in the setter property, and then use a "proxy" in the setter value to set up the binding between the actual property of the UI element and the object from the data binding. this technique can also be used for things like wiring up command to react to user input, for example when a user double clicks a listboxitem.

The following example shows how to set the background color of a listboxitem through data binding, and also how to react to a double click on the item.

 <  ListBox X  :  Name  = "Listbox1"> <  ListBox. itemtemplate  > <  Datatemplate  > <  Textblock  Text  = "{  Binding  Path  = Name} "/> </  Datatemplate  > </ ListBox. itemtemplate  > <  ListBox. itemcontainerstyle  > <  Style  Targettype  = "Listboxitem"> <  Setter  Property  = "B: bindingbehavior. propertybinding"> <  Setter. Value  > <  B  : Propertybinding  Binding  = "{  Binding  Path  = Backgroundbrush }"  Property  = "Background"/> </  Setter. Value  > </  Setter  > <  Setter  Property = "B: bindingbehavior. doubleclickcommandbinding"> <  Setter. Value  > <  B  :  Commandbinding  Command  = "{  Binding  Path  = Datacontext. testcommand,  Elementname  = Layoutroot }"  Commandparameter = "{  Binding  } "/> </  Setter. Value  > </  Setter  > </  Style  > </  ListBox. itemcontainerstyle  > </  ListBox  > 

As you can see, both the setter properties have attached properties defined instead of the actual property names of the listboxitem. in the setter values, I have added my proxy classes where I have specified the bindings and the name of the listboxitem property I want a binding to apply.

The bindingproperty proxy class is very simple and contains only two properties; one for holding the name of the property you want to bind to and one for the binding itself.

 
Public classPropertybinding{PublicBindingBinding {Get;Set;}Public StringProperty {Get;Set;}}

The bindingbehavior class is responsible for creating the actual binding based on an instance of the propertybinding. it does so using reflection on the listboxitem to locate the dependency property specified in the property attribute of the propertybinding proxy instance.

 Private Static void Onpropertybindingchanged ( Dependencyobject D, Dependencypropertychangedeventargs E ){ Frameworkelement Frameworkelement = d As  Frameworkelement ; If (Frameworkelement =Null ){ Throw new  Argumentexception ( "The propertybinding behavior can only be applied to frameworkelement types" );} Propertybinding Stylebinding = E. newvalue As  Propertybinding ; If (Stylebinding! = Null ){ String Deppropname = String . Concat (stylebinding. property, "Property" ); If (Deppropname. indexof ( '.' )>-1 ){ Int Index = deppropname. lastindexof ( '.' ); Deppropname = deppropname. substring (INDEX );} Fieldinfo Dependencypropertyfield = frameworkelement. GetType (). getfield (deppropname, Bindingflags . Public | Bindingflags . Static |Bindingflags . Flattenhierarchy ); If (Dependencypropertyfield! = Null ){ Dependencyproperty Dependencyproperty = dependencypropertyfield. getvalue ( Null ) As  Dependencyproperty ; If (Dependencyproperty! = Null ) {Frameworkelement. setbinding (dependencyproperty, stylebinding. Binding );}}}}   
 
For the command binding, a similar approach is used. the commandbinding proxy class contains a binding property for setting the command and commandparameter, and then the bindingbehavior class creates the actual binding in the onxxxchanged event handler.
Public classCommandbinding{PublicBindingCommand {Get;Set;}PublicBindingCommandparameter {Get;Set;}}
 Private Static void Ondoubleclickcommandbindingchanged ( Dependencyobject D, Dependencypropertychangedeventargs E ){ Frameworkelement Element = d As Frameworkelement ; If (Element = Null ){ Throw new  Invalidoperationexception ( "This behavior can only be applied to frameworkelement types" );} Commandbinding Stylebinding = E. newvalue As  Commandbinding ; If (Stylebinding! = Null ) {Element. setbinding ( Bindingbehavior . Doubleclickproperty, stylebinding. Command ); If (Stylebinding. commandparameter! = Null ) {Element. setbinding ( Bindingbehavior . Doubleclickparameterproperty, stylebinding. commandparameter );} Else {Element. clearvalue ( Bindingbehavior . Doubleclickparameterproperty );}} Else {Element. clearvalue (Bindingbehavior . Doubleclickproperty); element. clearvalue ( Bindingbehavior . Doubleclickparameterproperty );}}

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.