gpt4 book ai didi

wpf - ListBox 显示水平图像 WPF

转载 作者:行者123 更新时间:2023-12-01 09:04:44 25 4
gpt4 key购买 nike

我正在尝试在 wpf/xaml 中创建一个控件,该控件将显示图像的水平列表。要固定的列表框的宽度(无滚动条)。添加新项目时,现有项目会减少显示的图像数量以适应它(实际图像不会仅减少显示的图像数量)。该功能类似于将新列添加到具有相对宽度属性(“*”)的网格中,并且该列包含具有固定宽度的图像。到目前为止,这是我的代码:

<Window.Resources>
<ItemsPanelTemplate x:Key="ListBox_HorizontalItems">
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>

<DataTemplate x:Key="ListBox_DataTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Image Width="150" Source="{Binding ImageSource}" />
</Grid>
</DataTemplate>

<Style x:Key="ListBox_Style_Horizontal" TargetType="ListBox">
<Setter Property="Width" Value="150" />-->
<Setter Property="ItemTemplate" Value="{StaticResource ListBox_DataTemplate}" />
<Setter Property="ItemsPanel" Value="{StaticResource ListBox_HorizontalItems}" />
</Style>
</Window.Resources>

<Grid>
<ListBox Name="lbxImages" Style="{StaticResource ListBox_Style_Horizontal}" Width="250" Height="100" />
</Grid>

这非常接近我的需要!但是,当将新项目添加到列表中时,我无法弄清楚如何减少显示的图像数量。当前添加新项目时会出现滚动条。如果我没有很好地解释自己,这里有一些屏幕截图显示了我需要的功能:

3 Items 4 Items 5 Items

谁能告诉我如何做到这一点?感谢您的帮助!

最佳答案

使用以下 UniformGrid 作为 ItemsPanel:

<ItemsPanelTemplate>
<UniformGrid Columns="{Binding Path=Items.Count,RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"/>
</ItemsPanelTemplate>

禁用水平滚动:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">

修改项目模板:

<DataTemplate>
<Image Source="{Binding ImageSource}"
Stretch="None"
HorizontalAlignment="Center"/>
</DataTemplate>

关于wpf - ListBox 显示水平图像 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10172712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com