.Net Interview Questions and Answers

Search:

Explain multi binding and multivalue converters? (.NET WPF Interview questions with answers)

By : Shiv Prasad Koirala | Jan 13th, 2014 | Views : 4829 | Comments : 0 Bookmark and Share

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".


public class NameMultiConverter : IMultiValueConverter

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    

// Conversion logic from Target to Source  

}

}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)


// Conversion logic 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.


public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)

       

            string x = "";

            foreach (object y in values)

           

 

                x = x + " " + y.ToString();

            }

            return x;

        }

 


"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[0]) will go to the first textbox and the second array index (array[1]) will go to the second text box.


public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)

       

            string str = (string)value;

            string[] val = str.Split(' ');

 

 

            return val;

 

        }


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.


<TextBox x:Name="txtFirstName" HorizontalAlignment="Left" Height="26" Margin="42,67,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="152"/>


<TextBox x:Name="txtLastName" HorizontalAlignment="Left" Height="26" Margin="41,135,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="153"/>

 

<TextBox x:Name="txtFirstAndLast"  Height="28" HorizontalAlignment="Left" Margin="239,103,0,0" Name="label1" VerticalAlignment="Top" Width="117">

<TextBox.Text>

<MultiBinding Converter="StaticResource NameMultiConverter}">

<Binding ElementName="txtFirstName" Path="Text" />

<Binding ElementName="txtLastName" Path="Text" />

</MultiBinding>

</TextBox.Text>

</TextBox>


Below video explain what is WPF and how they differ from Windows form


 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi