如何将数据和填充/显示从列表框中选择的项绑定到DataGrid或UniformGrid (SelectionMode=M



关于我的问题(标题),我已经可以从我的listBox中绑定选定的项目到我的uniformGrid。BUT即使我已经选择了许多项,UniformGrid也只显示1项。

你能告诉我怎么做吗?

是否可以用所选的ListBox项填充UniformGrid ?

从列表框中转移(绑定)和显示所选项目的其他选项是什么?

如果您有类似的示例,我将直接遍历代码。

确切地说,我的ListBox项目是图像,但不需要只有图像。我只是想知道如何将选中的项目绑定到网格或任何将显示我的列表框选中的项目。

谢谢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>

我看到您将griddatacontext绑定到ListBox SelectedItem,它总是一个。这就是为什么在网格中只能看到一个项目。(我猜总是最后被选中的)。为了以更准确的MVVM方式解决这个问题,我个人会添加一个新的可观察集合ListBoxSelecetedItems,将其绑定到DataGrid,并在每个ListBox选择上添加新的selecteditem到该集合。如果从列表框中取消选择项目,请将其从集合中删除。

最新更新