这是我解决这个问题的方法(我正在回答我自己的问题)。
首先是我使用的 Multibinding TypeConverters:
public class CombineFullNameAndPhoneExtensionMultiConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (values[0] as string != null)
{
string fullName = (string)values[0] ?? "Unknown";
string phoneExtension = (string)values[1] ?? "Unknown";
string namePlusExtension = fullName.Trim() + " Phone: " + phoneExtension.Trim();
return namePlusExtension;
}
return null;
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
NotesContact c = (NotesContact)value;
string[] returnValues = { c.FullName.Trim(), c.PhoneExtension.Trim() };
return returnValues;
}
}
public class CombineLastNameFirstNameAndPhoneExtensionMultiConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (values[0] as string != null)
{
string lastName = (string)values[0] ?? "Unknown";
string firstName = (string)values[1] ?? "Unknown";
string phoneExtension = (string)values[2] ?? "Unknown";
string lastCommaFirstPlusExtension = lastName.Trim() + ", " + firstName.Trim() + " Phone: " + phoneExtension.Trim();
return lastCommaFirstPlusExtension;
}
return null;
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
以下是 xaml 中的 Converter 声明:
<local:CombineFullNameAndPhoneExtensionMultiConverter x:Key="combinedFullNameAndPhoneExtensionConverter"/>
<local:CombineLastNameFirstNameAndPhoneExtensionMultiConverter x:Key="combinedLastNameFirstNameAndPhoneExtensionConverter"/>
现在这里是 ComboBox xaml:
<telerik:GridViewComboBoxColumn Name="contactsGridViewComboBox" Header="Contact"
ItemsSource="{Binding ContactListObservable, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}">
<telerik:GridViewComboBoxColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource combinedFullNameAndPhoneExtensionConverter}">
<Binding Path="FullName"/>
<Binding Path="PhoneExtension"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</telerik:GridViewComboBoxColumn.CellTemplate>
<telerik:GridViewComboBoxColumn.CellEditTemplate>
<DataTemplate x:Name="ContactsCellEditTemplate">
<Grid FocusManager.FocusedElement="{Binding ElementName=ContactsTemplateComboBox}">
<telerik:RadComboBox x:Name="ContactsTemplateComboBox" IsSynchronizedWithCurrentItem="False" IsEditable="False" ItemsSource="{Binding ContactListObservable, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" IsDropDownOpen="True" TextSearchMode="Contains" IsFilteringEnabled="True" OpenDropDownOnFocus="True">
<telerik:RadComboBox.SelectedItem>
<MultiBinding Converter="{StaticResource combinedFullNameAndPhoneExtensionConverter}">
<Binding Path="FullName" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="PhoneExtension" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</telerik:RadComboBox.SelectedItem>
<telerik:RadComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource combinedLastNameFirstNameAndPhoneExtensionConverter}">
<Binding Path="LastName"/> <Binding Path="FirstName" />
<Binding Path="PhoneExtension"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
</telerik:RadComboBox>
</Grid>
</DataTemplate>
</telerik:GridViewComboBoxColumn.CellEditTemplate>
请注意,我使用了Multibinding、CellTemplate(在查看模式下获取或设置单元格的数据模板)和CellEditTemplate(在编辑模式下获取或设置单元格的数据模板)。由于我的时间不多了,我让代码自己说话。如果您需要澄清,请发表评论,如果这太令人困惑,请发布新的 Stack Overflow 问题。我非常感谢 SO 社区。你摇滚!