WPF 组合框中的不同值错误
我有当前的组合框 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="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();
}
}
我已将以下内容添加到我的资源中:
<convert:DistinctConverter x:Key="distinctConverter" />
问题是,我在组合框中收到错误:
任何人都可以帮助我解决我在这里做错的事情。
I have the current Combo Box 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="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>
And I have the class - 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();
}
}
I have added the following to my resources:
<convert:DistinctConverter x:Key="distinctConverter" />
The problem is, I'm getting the error in my combo box:
Can anyone help me with whatever I'm doing wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是模型中的
showName
属性返回一个集合,您希望将其绑定到TextBox
的Text
属性,该属性是细绳。然后,您有一个转换器,它接受一个集合作为输入,对其运行 LINQ 查询,该查询返回另一个集合。 该值(整个集合)通过使用ToString
绑定到字符串进行转换,并在组合框中显示为单个条目。然后对组合框中的每个项目重复该过程。如果不确切知道您想要实现什么目标,就很难准确地建议如何解决这个问题。例如,如果
showName
等于:您希望它出现在组合框行中吗?
如果是这样,那么您可以在使用
Distinct
后使用Aggregate
。但听起来您更有可能希望 Bill、Mike 和 Ted 在组合框中显示为单独的项目。在这种情况下,您需要将转换器应用到
ComboBox
本身的ItemsSource
,而不是ItemTemplate
中的TextBox
>。The problem is that the
showName
property in your model is returning a collection which you want to bind to theText
property of aTextBox
which is a string. Then you have a converter that takes a collection as input, runs a LINQ query on it, which returns another collection. That value, the whole collection, is being converted by the binding to a string usingToString
and being displayed as a single entry in your combo box. And then that process is repeated for each item in the combo box.Without knowing exactly what you are trying to accomplish, it's hard to suggest exactly how to fix this. For example, if
showName
is equal to:Would you like this to appear in the combo box row?
If so, then you can use
Aggregate
after you useDistinct
.But it sounds more likely that you want Bill, Mike and Ted to appear as separate items in the combo box. In that case you need to apply the converter to the
ItemsSource
for theComboBox
itself instead of theTextBox
in theItemTemplate
.