WPF -使用ItemsControl创建ImageGrid?

qf9go6mv  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(97)

很抱歉用一个初学者的问题来打扰你。我试图用C#中的WPF实现一个图像网格(比如在漫画浏览器或电影数据库中)。
现在,我尝试使用ListView和ListBox最初,但我没有得到它的工作有一个简单的样式图像网格。然后我偶然发现一个ItemsControl与一个WrapPanel组合。这很容易工作。
然后我试图实现一个方法,打开点击的图像,我卡住了。现在我发现在各种文章中,ItemsControl不提供动作来读取当前选定的项目索引。
现在我有点失望了。你有一个简单的ImageGrid或Idea的示例代码来使用ItemsControl吗?我唯一的想法是读取sender对象的Image源,然后将其与图像列表进行比较。
下面是我的XAML代码:

<ItemsControl Name="ImageHistory" Margin="100" HorizontalAlignment="Center" HorizontalContentAlignment="Center">
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel Margin="15" MouseUp="OpenDirectory" MouseEnter="HighlightButton" MouseLeave="UnhighlightButton">
            <Image Source="{Binding ImageData}" Height="150" Width="150" Margin="10"
                RenderOptions.BitmapScalingMode="HighQuality" Stretch="UniformToFill"/>
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>

字符串
提前感谢!!

oxalkeyp

oxalkeyp1#

好的,我做到了。只要我问对了问题,它就很简单。删除滚动功能就成功了。下面是工作代码:

<ListBox Name="FolderList" Margin="100" HorizontalAlignment="Center" HorizontalContentAlignment="Center"  ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
    <ItemsPanelTemplate >
        <WrapPanel/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Background="{DynamicResource ButtonDefault}" Margin="25"
                MouseUp="LoadFolder" MouseEnter="ButtonLight_MouseEnter" MouseLeave="ButtonLight_MouseLeave">
            <Image Source="{Binding ImageData}" Stretch="UniformToFill" RenderOptions.BitmapScalingMode="HighQuality"  
                Width="250" Height="250" Margin="25"/>
            <DockPanel Margin="0,25,0,25">
                <TextBlock Text="{Binding Caption}" Width="400" Margin="10" DockPanel.Dock="Top"
                    FontFamily="Perpetua Titling MT" FontSize="24" Foreground="{DynamicResource TextColorHeader}"/>
                <TextBlock Text="{Binding FolderPath}" Width="400" Margin="10" TextWrapping="Wrap" DockPanel.Dock="Bottom"
                    FontFamily="Lato Light" FontSize="16" Foreground="{DynamicResource TextColorSubtext}" FontStyle="Italic"/>
                <TextBlock Text="{Binding Subtext}" Width="400" Margin="10" TextWrapping="Wrap" DockPanel.Dock="Top"
                    FontFamily="Lato Light" FontSize="16" Foreground="{DynamicResource TextColorSubtext}"/>
            </DockPanel>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

字符串
无论如何,非常感谢:)

相关问题