есть у меня разметка примерно такая:
<TextBox Name="nameTBox" MinWidth="200" />
...
<TextBox Name="lastNameTBox" MinWidth="200" />
...
<TextBox Name="patronymicTBox" MinWidth="200" />
...
<TextBox Name="fullNameTBox" MinWidth="200">
<TextBox.Text>
<MultiBinding Converter="{StaticResource FullNameConverter}">
<Binding ElementName="nameTBox" Path="Text" Converter="{StaticResource NameValuePairConverter}" ConverterParameter="Name" />
<Binding ElementName="lastNameTBox" Path="Text" Converter="{StaticResource NameValuePairConverter}" ConverterParameter="LastName" />
<Binding ElementName="patronymicTBox" Path="Text" Converter="{StaticResource NameValuePairConverter}" ConverterParameter="Patronymic" />
</MultiBinding>
</TextBox.Text>
</TextBox>
А вот код конвертеров
public class NameValuePair
{
public string PairName
{
get ; set;
}
public object PairValue
{
get ; set ;
}
public NameValuePair()
{
}
public NameValuePair(string pairName, object pairValue)
{
this.PairName = pairName;
this.PairValue = pairValue;
}
}
public class NameValuePairConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string name = parameter as string;
return new NameValuePair(name, value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
public class FullNameConverter : DependencyObject, IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
NameValuePair[] arr = values.OfType<NameValuePair>().ToArray<NameValuePair>();
string name = string.Empty;
string lastName = string.Empty;
string patronymic = string.Empty;
var q = (from it in arr where it.PairName == "Name" select it).ToList();
name = (q.Count > 0) ? q[0].PairValue as string : name;
q = (from it in arr where it.PairName == "LastName" select it).ToList();
lastName = (q.Count > 0) ? q[0].PairValue as string : lastName;
q = (from it in arr where it.PairName == "Patronymic" select it).ToList();
patronymic = (q.Count > 0) ? q[0].PairValue as string : patronymic;
string fullName = string.Format("{0} {1} {2}", name, lastName, patronymic);
return fullName;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
ВНИМАНИЕ ВОПРОС.
Слышал про проблему того что перед тем как передать значение в Converter элемента MultiBinding значения конвертируются в тип Target свойства. Но почему в методе Convert FullNameConverter-а я получаю массив из 3 DependencyProperty.UnsetValue значений с типом MS.Internal.NamedObject, а не null-ы?
И еще, есть ли возможность заставить такой код пахать под данной версией WPF?
Цель кода извлекать в IMultiValueConverter-е параметры по именам а не по индексам.
Заранее спасибо