Reputation: 11
i m using mvvm pattern with wpf and Entity framework and implemented INotifyPropertyChanged in viewmodel and model
im using Multibinding with 3 textbox control, first textbox for entry first name secend textbox for last name
and third textbox for Full name
third textbox using multibinding:
<TextBox >
<TextBox.Text>
<MultiBinding Converter="{StaticResource FullNameConverter}">
<Binding Path="PersonData.FullName" Mode="TwoWay" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"/>
<Binding ElementName="FirstNameTextBox" Path="Text" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" />
<Binding ElementName="LastNameTextBox" Path="Text" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
this is my convertor
public class FullNameConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("{0} {1}", values[1], values[2]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new[] { value };
}
}
this work perfectly and save FirstName And LastName to database but not save FullName to database with multibinding.
but when entry Directly in FullName TextBox Save Data To database
how to resolve this problem? please help me thanks
Upvotes: 0
Views: 275
Reputation: 11
Thank you sheridan FullName Infact is a alias of First and last name. Example:if First Name is Lionel And LastName is Messi, Multibinding Full Name=Lionel Messi But Sometimes i want change Full Name to Custom Nmae ,Example:Lionel Messi To Lio
Upvotes: 0
Reputation: 69979
MultiBinding
s do not work in the way that you seem to think they do. You cannot use one to create a new value to save in the database. They can only be used to create a new value to display in the UI. If you want to create a new value to save in the database, then you'll need to create property for that in code... perhaps something like this?:
public string Fullname
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
Of course, to do this, you would also need to have properties named FirstName
and LastName
which are data bound to the first two TextBox
es, but then you're supposed to be doing that anyway. In this case, you should also data bind this aggregated property to the third TextBox.Text
property in the UI:
<TextBox Text="{Binding FullName}" />
Please read the Data Binding Overview page on MSDN to find out more about how you should be writing your WPF code.
Upvotes: 2