Is there a way to bind 2 (or more) WPF items to a single property in C#? -


my class contains address property (string type), wpf window contains few text boxes address (house number, street name , optional second line of address - town , postcode have separate fields in class). them single address string in class, separated commas (eg. "43, whitefield road, princesshire"). there way binding, or manually in code?

enter image description here

bind fields address property on class, use value converter determine components of address applicable. in converter have property allowing determine substring manipulate:

public class addressseparatorconverter : ivalueconverter {     ///<summary>     /// index of substring element of address manipulated (read/edited).     ///</summary>     public int substringidx { get; set; }      ... } 

then when perform conversion, split string using , delimiter , return substring specified substringidx:

address.split(',')[substringidx] 

as you're binding editable control, need make sure provide implementations both ivalueconverter.convert , ivalueconverter.convertback methods.

to take 1 step further, provide different converter definitions in converters.xaml file handle various subcomponents of address:

<converters:addressseparatorconverter x:key="addresstohousenumberconverter" substringidx="0"/> <converters:addressseparatorconverter x:key="addresstostreetnameconverter" substringidx="1"/> ... 

then when binding control call appropriate converter key:

<textbox text="{binding address, converter={staticresource addresstostreetnameconverter}}"/> 

i don't have access ide right please accept code snippets may not 100% right should gist of it.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -