How do I add multiple images in StackPanel WPF from Folder?

后端 未结 1 845
后悔当初
后悔当初 2020-12-01 23:18

I want to give folder path and from that folder path If That folder contains 3 images I want to display those 3 images into Stac

相关标签:
1条回答
  • 2020-12-01 23:50

    You should use an ItemsControl like shown below. It uses a vertical StackPanel as default panel for its items.

    <ItemsControl x:Name="imageItems">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding}" Margin="5"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    Set the ItemsSource of the ItemsControl like this:

    imageItems.ItemsSource = Directory.EnumerateFiles(FOLDERPATH, "*.png");
    

    The conversion from path string to ImageSource is performed by built-in type conversion in WPF.


    You may use a different ItemsPanel like this:

    <ItemsControl ...>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        ...
    </ItemsControl>
    
    0 讨论(0)
提交回复
热议问题