0

至于我的问题(标题),我已经可以将列表框中的选定项目绑定到我的uniformGrid。 但是即使我已经选择了许多项目,UniformGrid 也只显示 1 个项目。

你能告诉我怎么做吗?

或者 是否可以用选定的 ListBox 项目填充 UniformGrid?

或者 还有哪些其他选项可以传输(绑定)并从我的列表框中显示我的选定项目?

或者 ,如果您有类似的示例,我将直接浏览代码。

确切地说,我的 ListBox 项目是图像,但不需要只是图像。我只想知道如何将所选项目绑定到 Grid 或任何将显示我的 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>

4

1 回答 1

0

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

希望这可以帮助。

于 2011-12-03T09:35:07.237 回答