WPF 列表框多选绑定

发布于 2024-10-12 08:18:02 字数 1610 浏览 0 评论 0原文

我有两个列表框,一左一右。当我选择左侧列表框中的“contactList”项目时,“标签”信息应显示在右侧列表框中,并且该部分工作正常。我遇到的问题与多选有关,因为目前它只会显示一项选择中的信息。我将 XAML 中的选择模式更改为多选,但这似乎不起作用。将不胜感激任何帮助。谢谢。

XAML

<Grid x:Name="LayoutRoot" Background="#FFCBD5E6">
    <ListBox x:Name="contactsList" SelectionMode="Multiple" Margin="7,8,0,7" ItemsSource="{Binding ContactLists, Mode=Default}" ItemTemplate="{DynamicResource ContactsTemplate}" HorizontalAlignment="Left" Width="254" SelectionChanged="contactsList_SelectionChanged"/>
    <ListBox x:Name="tagsList" Margin="293,8,8,8" ItemsSource="{Binding AggLabels, Mode=Default}" ItemTemplate="{StaticResource TagsTemplate}" Style="{StaticResource tagsStyle}" />
</Grid>

代码

private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (contactsList.SelectedItems.Count > 0)
        {
            CollectionViewGroup collectionView = contactsList.SelectedItems[0] as CollectionViewGroup;
            ContactList selectedContact = contactsList.SelectedItems[0] as ContactList;

            ObservableCollection<AggregatedLabel> labelList = new ObservableCollection<AggregatedLabel>();

            foreach (ContactList contactList in collectionView.Items)
            {
                foreach (AggregatedLabel aggLabel in contactList.AggLabels)
                {
                    labelList.Add(aggLabel);

                    tagsList.ItemsSource = labelList;

                }

            }
        }
    }

I have two listBoxes one on the left and one on the right. When I select a 'contactList' item on the left listBox the 'label' information should be displayed on the right listBox and this part works fine. The problem I am having is to do with multi-select because at the moment it will only display the information from one selection. I changed Selection mode in my XAML to multi-select but that did not seem to work. Would appreciate any assistance. Thanks.

XAML

<Grid x:Name="LayoutRoot" Background="#FFCBD5E6">
    <ListBox x:Name="contactsList" SelectionMode="Multiple" Margin="7,8,0,7" ItemsSource="{Binding ContactLists, Mode=Default}" ItemTemplate="{DynamicResource ContactsTemplate}" HorizontalAlignment="Left" Width="254" SelectionChanged="contactsList_SelectionChanged"/>
    <ListBox x:Name="tagsList" Margin="293,8,8,8" ItemsSource="{Binding AggLabels, Mode=Default}" ItemTemplate="{StaticResource TagsTemplate}" Style="{StaticResource tagsStyle}" />
</Grid>

Code

private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (contactsList.SelectedItems.Count > 0)
        {
            CollectionViewGroup collectionView = contactsList.SelectedItems[0] as CollectionViewGroup;
            ContactList selectedContact = contactsList.SelectedItems[0] as ContactList;

            ObservableCollection<AggregatedLabel> labelList = new ObservableCollection<AggregatedLabel>();

            foreach (ContactList contactList in collectionView.Items)
            {
                foreach (AggregatedLabel aggLabel in contactList.AggLabels)
                {
                    labelList.Add(aggLabel);

                    tagsList.ItemsSource = labelList;

                }

            }
        }
    }

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

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

发布评论

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

评论(2

沫雨熙 2024-10-19 08:18:02

我想每个人都对这部分感到困惑,

CollectionViewGroup collectionView = contactsList.SelectedItems[0] as CollectionViewGroup;
ContactList selectedContact = contactsList.SelectedItems[0] as ContactList;

因为你只看第一个选定的项目。 (SelectedItems[0]),但将其视为一件事还是另一件事?

理想情况下,您可能需要类似的东西

// only create the list once, outside all the loops
ObservableCollection<AggregatedLabel> labelList = new ObservableCollection<AggregatedLabel>();

foreach (var selected in contactsList.SelectedItems)
{
   // pretty much your existing code here, referencing selected instead of SelectedItems[0]
}

// only set the list once, outside all the loops
tagsList.ItemsSource = labelList;

,您不会在tagsList上设置项目源,您已经将其绑定到集合,并且您只需替换此方法中的内容。 (只需一次调用来清除顶部的集合,并且无需调用设置 ItemsSource,因为它已经被绑定)

I think everyone is confused about this part

CollectionViewGroup collectionView = contactsList.SelectedItems[0] as CollectionViewGroup;
ContactList selectedContact = contactsList.SelectedItems[0] as ContactList;

you're only looking at the first selected item. (SelectedItems[0]), but treating it as one thing or another?

you probably need something like

// only create the list once, outside all the loops
ObservableCollection<AggregatedLabel> labelList = new ObservableCollection<AggregatedLabel>();

foreach (var selected in contactsList.SelectedItems)
{
   // pretty much your existing code here, referencing selected instead of SelectedItems[0]
}

// only set the list once, outside all the loops
tagsList.ItemsSource = labelList;

ideally, you wouldn't be setting the items source on the tagsList, you'd have that bound to a collection already, and you'd just be replacing the contents in this method. (just one call to clear the collection at the top, and no call to set ItemsSource, since it would have already been bound)

掀纱窥君容 2024-10-19 08:18:02

我真的不明白你在用所有这些代码做什么,但是你通常如何处理你描述的那种场景是将第二个列表框直接绑定到第一个列表框,应该看起来像这样:

<ListBox Name="ListBox1" ItemsSouce="{Binding SomeOriginalSource}" .../>
<ListBox ItemsSouce="{Binding ElementName=ListBox1, Path=SelectedItems}".../>

编辑:然后可以使用枚举内部集合的 DataTemplate(例如,这可能会导致您拥有一个包含其他列表框的列表框),或者向绑定添加一个转换器,将内部集合合并到单个集合中,如 John Gardner 指出的那样。

I don't really get what you are doing there at all with all that code but how you normally approach the kind of scenario you described is by binding the second ListBox directly to the first one, should look something like this:

<ListBox Name="ListBox1" ItemsSouce="{Binding SomeOriginalSource}" .../>
<ListBox ItemsSouce="{Binding ElementName=ListBox1, Path=SelectedItems}".../>

Edit: You then can either use a DataTemplate which enumerates the internal collections (which for example could cause you to have a ListBox containing other ListBoxes), or you add a converter to the the binding which merges the internal collections into a single collection like John Gardner noted.

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