0

私は BitmapSources の ObservableCollection を持っており、それらをすべてグリッドに表示し、選択されたスタイルと選択されていないスタイルをオーバーライドしたいと考えています。これを行うためにさまざまな方法を検討してきましたが、満足のいくように機能させることができませんでした。私の最新の試みは次のようになります。

<ListBox ItemsSource={Binding Images}>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel ItemsHost="True">
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Black" BorderThickness="3">
                <Image Source={Binding}>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

これは画像を表示しますが、WrapPanel は常に 1 行です... 水平方向にスクロールする必要はありません。行ごとのアイテムなど。WrapPanel がないと、画像はそれぞれ 1 行になります。また、DataTemplate の DataType が ListBoxItem ではなく BitmapSource になっているため、選択したアイテムなどのスタイルをオーバーライドする方法がよくわかりません...

同様の結果で DataGrid (より適切と思われる) も試しました。

どうすればいいですか?

4

3 に答える 3

1

-Edit-
The part about listbox width in this answer is not correct, see clemens answer instead

-Original post-
You need to constrict the width of the list box, either by setting the width or with some other parent layout. Otherwise the wrap panel will claim all the available width to put everything on one row. This happens to me alot when using scrollpanes for example

One tip is to setting different background colors to elements to see who is actually grabbing the space

You can change the selected styles by setting the ItemsContainerStyle

于 2013-10-10T09:16:00.507 に答える
1

水平スクロールを無効にするだけです。これにより、WrapPanel がその子をラップします。

<ListBox ItemsSource="{Binding Images}"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Black" BorderThickness="3">
                <Image Stretch="None" Source="{Binding}"/>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
于 2013-10-10T09:28:25.507 に答える