WPF 组合框中的不同值错误

发布于 2024-11-07 06:12:01 字数 1569 浏览 0 评论 0原文

我有当前的组合框 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:

enter image description here

Can anyone help me with whatever I'm doing wrong here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2024-11-14 06:12:01

问题是模型中的 showName 属性返回一个集合,您希望将其绑定到 TextBoxText 属性,该属性是细绳。然后,您有一个转换器,它接受一个集合作为输入,对其运行 LINQ 查询,该查询返回另一个集合。 值(整个集合)通过使用ToString绑定到字符串进行转换,并在组合框中显示为单个条目。然后对组合框中的每个项目重复该过程。

如果不确切知道您想要实现什么目标,就很难准确地建议如何解决这个问题。例如,如果 showName 等于:

string[] { "Bill", "Bill", "Mike", "Ted" };

您希望它出现在组合框行中吗?

比尔·迈克·泰德

如果是这样,那么您可以在使用 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 the Text property of a TextBox 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 using ToString 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:

string[] { "Bill", "Bill", "Mike", "Ted" };

Would you like this to appear in the combo box row?

Bill Mike Ted

If so, then you can use Aggregate after you use Distinct.

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 the ComboBox itself instead of the TextBox in the ItemTemplate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文