C# WPF组合框中的不同值错误
我有当前的组合框XAML:C# WPF组合框中的不同值错误,c#,silverlight,xaml,combobox,ienumerable,C#,Silverlight,Xaml,Combobox,Ienumerable,我有当前的组合框XAML: <ComboBox Height="23" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=showDomainDataSource, Path=Data}" Margin="583,8,0,0" x:Name="showsComboBox" VerticalAlignment="Top" Width="233" SelectionChanged="showsComboBo
<ComboBox Height="23" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=showDomainDataSource, Path=Data}" Margin="583,8,0,0" x:Name="showsComboBox" VerticalAlignment="Top" Width="233" SelectionChanged="showsComboBox_SelectionChanged" IsSynchronizedWithCurrentItem="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=showName, Converter={StaticResource distinctConverter}}" x:Name="showsComboxshowName" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我有一个类-DistinctConverter:
public class DistinctConverter : IValueConverter
{
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
var values = value as IEnumerable;
if (values == null)
return null;
return values.Cast<object>().Distinct();
}
public object ConvertBack(
object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
公共类DistinctConverter:IValueConverter
{
公共对象转换(
对象值、类型targetType、对象参数、CultureInfo区域性)
{
变量值=作为IEnumerable的值;
如果(值==null)
返回null;
返回值.Cast().Distinct();
}
公共对象转换回(
对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotSupportedException();
}
}
我已将以下内容添加到我的资源中:
<convert:DistinctConverter x:Key="distinctConverter" />
问题是,我的组合框中出现了错误:
有谁能帮我解决我这里做错了什么。问题是,模型中的
showName
属性返回了一个集合,您希望将该集合绑定到TextBox
属性,该属性是一个字符串。然后您有一个转换器,它接受一个集合作为输入,对它运行LINQ查询,然后返回另一个集合通过使用ToString
绑定将整个集合的值转换为字符串,并在组合框中显示为单个条目。然后对组合框中的每个项目重复该过程
如果不确切知道您想要完成什么,就很难确切地建议如何解决这个问题。例如,如果showName
等于:
string[] { "Bill", "Bill", "Mike", "Ted" };
是否要将其显示在组合框行中
比尔·迈克·特德
如果是这样,那么在使用Distinct
之后,可以使用Aggregate
但听起来你更可能希望比尔、迈克和泰德在组合框中作为单独的项目出现。在这种情况下,您需要将转换器应用于
组合框
本身的项源
,而不是项模板中的文本框
。感谢您提供的信息,您认为我想要不同的值是正确的。当输入:ItemsSource=“{Binding ElementName=showDomainDataSource,Path=Data,Converter={StaticResource distinctConverter}}}}”~~作为ItemsSource时,组合框返回空值,您知道怎么了吗?