Horizontal orientated WrapPanel within ItemsControl lists vertically

后端 未结 1 1157
走了就别回头了
走了就别回头了 2021-02-08 20:34

I have two DataTemplates defined within my XAML, each used for a seperate ItemsControl panel.

The main ItemsControl lists Foo objects stored within an ObservableCollecti

相关标签:
1条回答
  • 2021-02-08 20:57

    I think its because you are adding each image item to a new WrapPanel in GameImagesTemplate , you should just have to set the ItemsControl ItemsPanelTemplate to WrapPanel in the GameTemplate

    Example:

    Xaml:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="252.351" Width="403.213" Name="UI" >
        <Window.Resources>
    
            <DataTemplate x:Key="GameImagesTemplate" >
                <StackPanel>
                    <Image Source="{Binding FileInfo.FullName}" Margin="8,8,8,8" Height="70" Width="70" />
                    <Label Content="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
    
            <DataTemplate x:Key="GameTemplate">
                <StackPanel>
                    <Label Content="{Binding Name}" Grid.Row="0" Background="Gray" FontSize="16" />
                    <ItemsControl x:Name="imageContent" ItemsSource="{Binding FileList}" ItemTemplate="{StaticResource GameImagesTemplate}" >
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
    
        <Grid>
            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                <ItemsControl ItemsSource="{Binding ElementName=UI, Path=FileList}" Grid.Column="0" ItemTemplate="{StaticResource GameTemplate}" />
            </ScrollViewer>
        </Grid>
    </Window>
    

    Code:

    public partial class MainWindow : Window
    {
        private ObservableCollection<Foo> _fileList = new ObservableCollection<Foo>();
    
        public MainWindow()
        {
            InitializeComponent();
            foreach (var item in Directory.GetDirectories(@"C:\StackOverflow"))
            {
                FileList.Add(new Foo
                {
                    Name = item,
                    FileList = new ObservableCollection<Bar>(Directory.GetFiles(item).Select(x => new Bar { FileInfo = new FileInfo(x) }))
                });
            }
        } 
    
        public ObservableCollection<Foo> FileList
        {
            get { return _fileList; }
            set { _fileList = value; }
        }
    }
    
    public class Foo
    {
        public string Name { get; set; }
        public ObservableCollection<Bar> FileList { get; set; }
    }
    
    public class Bar
    {
        public FileInfo FileInfo { get; set; }
    }
    

    Result

    enter image description here

    0 讨论(0)
提交回复
热议问题