0

私の質問(タイトル)に関しては、選択したアイテムをlistBoxから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 を、常に 1 である ListBox SelectedItem にバインドしていることがわかります。そのため、グリッドにアイテムが 1 つしか表示されません。(私はそれが常に最後に選択されていると思います)。これをより正確な MVVM の方法で解決するために、私は個人的に、新しい監視可能なコレクション ListBoxSelecetedItems を追加し、それを DataGrid にバインドし、すべての ListBox 選択でそのコレクションに新しい selecteditem を追加します。リスト ボックスから項目を選択解除する場合は、コレクションから削除します。

お役に立てれば。

于 2011-12-03T09:35:07.237 に答える