简体   繁体   English

如何将数据绑定并填充/显示从ListBox选定项的DataGrid或UniformGrid(SelectionMode = Multiple)

[英]How to bind data and fill/display to DataGrid or UniformGrid from ListBox selected items (SelectionMode=Multiple)

As to my question(Title), I can already bind selected items from my listBox to my uniformGrid. 至于我的问题(标题), 我已经可以将列表框中的选定项目绑定到我的统一网格。 BUT even if I already selected many items the UniformGrid only shows 1 item. 但是,即使我已经选择了很多项目,UniformGrid也只显示1个项目。

Could you please tell me how to do this? 您能告诉我该怎么做吗?

or is it possible to fill the UniformGrid with the ListBox items selected? 还是可以用选定的ListBox项填充UniformGrid?

or What are the other options to transfer(bind) and show my selected items out from my ListBox? 还有什么其他选项可以转移(绑定)并从列表框显示我选择的项目?

or I'll just go and walk through the code if you have similar examples. 否则 ,如果您有类似的示例,我将仅遍历代码。

To be exact, my ListBox items are images BUT doesnt need to be images only. 确切地说,我的ListBox项是图像,但不必仅是图像。 I just want to know how to bind selected items to a Grid or anything that will display my ListBox selected items. 我只想知道如何将所选项目绑定到网格或将显示我的ListBox所选项目的任何内容。

Thank You 谢谢

XAML: XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="SampleBinding.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <StackPanel>
            <Image Source="{Binding myImages}" HorizontalAlignment="Left" Height="64" Width="64"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
    <ListBox x:Name="listBox" HorizontalAlignment="Left" ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}" Margin="19,40,0,102" Width="200" SelectionMode="Multiple"/>
    <UniformGrid x:Name="uGrid" DataContext="{Binding SelectedItem, ElementName=listBox}" Margin="273,40,78,132" d:DataContext="{Binding Collection[0]}" Grid.Row="2" Grid.Column="2">
        <Image x:Name="imageItem" Source="{Binding myImages}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100"/>
    </UniformGrid>
</Grid>

I see that you bind grids DataContext to ListBox SelectedItem, which is always one. 我看到您将网格DataContext绑定到始终为ListBox SelectedItem的列表。 That's why you see only one item in grid. 因此,您只能在网格中看到一个项目。 ( I guess it's always the last selected). (我猜它总是最后选择)。 To resolve this in more accurate MVVM way I, personally, would add a new observable collection ListBoxSelecetedItems, bind it to DataGrid, and on every ListBox selection add new selecteditem to that collection. 为了以更准确的MVVM方法解决此问题,我个人将添加一个新的可观察集合ListBoxSelecetedItems,将其绑定到DataGrid,然后在每个ListBox选择上将新的selecteditem添加到该集合。 If you unselect an item from list box, remove it from collection. 如果您从列表框中取消选择一项,则将其从集合中删除。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM