2012-03-02 68 views
0

现在我有一个列表框,里面有一个scrollviewer和一个stackpanel与数据绑定到imagelist的observablecollection。在列表框中绑定ObservableCollection到网格

我有一个photolist类,它保存图像和路径并将其绑定到列表框。

<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> 

上面的代码工作正常,显示列表框与一个滚动和stackpanel承载多个图像。

现在我想修改列表框以使scrollviewer和一个网格,而不是stackpanel,以便图像像矩阵形式定位。

请为我提供一个代码片段,将photolist绑定到网格(位于列表框内的滚动查看器中)。

任何帮助将不胜感激。

回答

1

你可以尝试使用WrapPanel,如果你需要的细胞都具有相同的尺寸用在这里的答案:WPF WrapPanel - all items should have the same width

+0

嗨scroog,我确实将stackpanel改为wrappanel,但是如果我有scrollviewer,wrappanel就像堆栈面板一样工作。如果我移除滚动查看器,图像会以网格方式填充。我如何让scrollviewer和wrappanel的行为像网格一样。 – Questions 2012-03-02 13:01:16

+0

@ Scrogg,改变scrollviewer的可见性后,事情就像预期的那样正常工作。感谢你的帮助。 – Questions 2012-03-02 13:06:01

+0

你可以尝试指定WrapPanel的宽度。 – Scroog1 2012-03-02 13:06:02

0

你需要改变你的风格来使用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> 
相关问题