Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ObservableCollection绑定到列表框内的网格_C#_Wpf - Fatal编程技术网

C# 将ObservableCollection绑定到列表框内的网格

C# 将ObservableCollection绑定到列表框内的网格,c#,wpf,C#,Wpf,现在我有一个列表框,里面有一个scrollviewer和一个stackpanel,其中的数据绑定到imagelist的observablecollection 我有一个photolist类,它保存图像和路径,并将其绑定到列表框 <Style TargetType="{x:Type ListBox}"> <Setter Property="Foreground" Value="White" /> <Setter Property="Margin" Value=

现在我有一个列表框,里面有一个scrollviewer和一个stackpanel,其中的数据绑定到imagelist的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

现在,我想修改listbox,使其具有scrollviewer和网格,而不是stackpanel,这样图像的位置就像矩阵形式一样

请提供一个代码片段,将照片列表绑定到网格(在列表框中的scrollviewer中)


非常感谢您的帮助。

您可以尝试使用包装,如果您要求单元格的大小都相同,请使用下面的答案:

您需要更改样式以使用包装:

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


Hi scroog,我确实将stackpanel更改为wrappanel,但是如果我有scrollviewer,wrappanel的工作原理与stackpanel一样。如果删除scrollviewer,图像将以网格方式填充。如何使scrollviewer和wrappanel都像grid一样工作。@Scrogg,在更改scrollviewer的可见性后,一切正常。感谢您的帮助。您可以尝试指定包装的宽度。