简体   繁体   English

如何在WPF中创建水平滚动图像网格

[英]How to create a horizontal scrolling image grid in wpf

I'm trying to implement an Image-Overview in my WPF-Application. 我正在尝试在WPF应用程序中实现图像概述。 For now, I've all Images beneath each other, with the opportunity to scroll verticaly, but I need them inside an horizontal scrolling grid of 3x(n/3) Images, where n is the total amount of images. 现在,我将所有图像放在彼此的下面,并有机会垂直滚动,但是我需要在3x(n / 3)个图像的水平滚动网格内,其中n是图像的总量。

<UserControl.Resources>
    <Style TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="4" CornerRadius="5" Margin="6" >
                        <Image Source="{Binding Path=UriSource}" Stretch="Fill" Width="100" Height="120" />
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<UserControl.DataContext>
    <ObjectDataProvider ObjectType="{x:Type local:FileCollection}" MethodName="LoadImages" />
</UserControl.DataContext>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>

    <ListBox ItemsSource="{Binding}" Grid.ColumnSpan="3" />
</Grid>

Actual Orientation is: 实际方向是:

H
H
H
.
.
.

After I followed pushprajs advice its: 在我跟随pushprajs建议之后:

HHH...

What i like to see is this: 我喜欢看的是:

HHHH
HHH
HHH

And again with real images: 再次使用真实图像:

how it looks for now: 现在看起来如何:

现在看起来如何

what i'd like to see: 我想看的是:

我想看什么

here you go 干得好

<ListBox ItemsSource="{Binding}"
         Grid.ColumnSpan="3">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

Idea is to change the items panel to a stack panel with horizontal orinetation 想法是将项目面板更改为水平排​​列的堆栈面板

3 rows across->vertical layout 3行->垂直布局

<ListBox ItemsSource="{Binding}"
         Grid.ColumnSpan="3">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="3" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

3 row vertical->across layout 3行垂直->跨布局

this is bit complex 这有点复杂

<Grid xmlns:l="clr-namespace:CSharpWPF">
    <Grid.Resources>
        <l:CollectionSplitter x:Key="CollectionSplitter" />
    </Grid.Resources>
    <ListBox Grid.ColumnSpan="3"
             ItemsSource="{Binding Collection,Converter={StaticResource CollectionSplitter}}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="1"
                                         Rows="3" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Black"
                                    BorderThickness="4"
                                    CornerRadius="5"
                                    Margin="6">
                                <Image Source="{Binding Path=UriSource}"
                                       Stretch="Fill"
                                       Width="100"
                                       Height="120" />
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

for above you need a converter as well 以上,您还需要一个转换器

namespace CSharpWPF
{
    class CollectionSplitter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            List<IEnumerable<object>> result = new List<IEnumerable<object>>();
            IEnumerable<object> collection = (value as IEnumerable).OfType<object>();
            for (int i = 0; i < collection.Count(); i+=3)
            {
                result.Add(collection.Skip(i).Take(3));
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

result 结果

结果

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

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