[英]Binding ObservableCollection to Grid Inside a Listbox
Right now I have a listbox inside which a scrollviewer and a stackpanel is placed with the data binding to the observablecollection of imagelist. 现在,我有一个列表框,其中放置了一个滚动查看器和一个堆栈面板,数据绑定到imagelist的observablecollection。
I am having a photolist class which holds the image and path of it and binding it to the listbox. 我有一个图片列表类,其中包含它的图像和路径,并将其绑定到列表框。
<Style TargetType="{x:Type ListBox}">
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="100"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}" >
<ScrollViewer>
<StackPanel IsItemsHost="True" Orientation="Horizontal" HorizontalAlignment="Center"/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The above code works fine showing the listbox with a scroller and stackpanel hosting the multiple images. 上面的代码可以很好地显示带有滚动条和堆栈面板的列表框,其中包含多个图像。
Now I would like to modify the listbox to have scrollviewer and a grid instead of stackpanel, so that the images are positioned like the matrix form. 现在,我想将列表框修改为具有scrollviewer和一个网格,而不是stackpanel,以使图像的位置像矩阵形式一样。
Please provide me a code snippet to bind the photolist to the grid(inside a scrollviewer which is inside a listbox). 请给我一个代码片段,将照片列表绑定到网格(在列表框内的scrollviewer内部)。
Any help would be highly appreciated. 任何帮助将不胜感激。
您可以尝试使用WrapPanel,如果您要求单元格的大小都相同,请使用以下答案: WPF WrapPanel-所有项目的宽度均应相同
You need to change your style to use the WrapPanel: 您需要更改样式才能使用WrapPanel:
<Style TargetType="{x:Type ListBox}">
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="100"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border BorderThickness="1" Margin="6">
<Image Source="{Binding Path=ImageUri}" Stretch="Fill" Width="100" Height="120" />
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
</Style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.