Explain multi binding and multivalue converters? (.NET WPF Interview questions with answers)
This question is taken from .NET interview question book published by BPB publications http://www.flipkart.com/net-interview-questions-6th/p/itmdyuqzdqx8cvqx
"MultiBinding" helps you bind multiple sources to a target while multi-converters acts like bridge if the source and targets have different data formats or need some conversion.
For example let's say you have two textboxes which has "FirstName" and "LastName". You want that as soon as users type on these two textboxes, the other text box should get updated with "FirstName LastName" ( As shown in the below figure).
Also vice versa if we type in the third text box "FirstName LastName" it should display "FirstName" and "LastName' in the other two textboxes respectively.
So the first thing is to create a multivalue converter class which will join "FirstName" and "LastName" in to source and split "FirstName LastName" back to target. For the same we need to implement "IMultiValueConverter" and implement "Convert" and "ConvertBack" methods."Convert" helps to do conversion from "Target" to "Source" and "ConvertBack" helps to convert from "Source" to "Target".
Below is the "Convert" (Target to Source) implementation where we get the "textbox" value in a array and we have concatenated the array values and returned the same in a single string. This single string will be displayed in the third textbox.
"ConvertBack" helps to implement conversion logic from "Source" to "Target". So when someone types "Shiv Koirala" in the third text box we would like to split "Shiv" in one string and "Koirala" in other string.
So you can see in the below code we have used the "Split" function to create a string array. So the first array index (array) will go to the first textbox and the second array index (array) will go to the second text box.
Now once we are done with the converter next thing is to apply multi-binding. Below is the XAML code where we have the three text boxes "txtFirstName" , "txtLastName" and "txtFirstAndLast".
You can see how the "txtFirstAndLast" textbox "Text" is binded using multibinding element which binds to "txtFirstName" and "txtLastName" textbox . So now if you make changes to multiple target the single source gets updated and if you update the source multiple target gets updates.
Below video explain what is WPF and how they differ from Windows form