在DataTemplate中排列ItemsControl的项

3

因为某些原因,使用 dataTemplate 添加的项不会按照我的要求执行操作!!

我试图在 StackPanel 中水平地放置多个图像,但无论我如何尝试,它们都只会垂直排列。

以下是我的 XAML 代码:

<DataTemplate x:Name="View">
  <Border BorderBrush="Red" BorderThickness="4" >
      <StackPanel  Orientation="Horizontal">
             <ItemsControl ItemsSource="{Binding Path=_Collection, Mode=OneWay}" >
                 <ItemsControl.ItemTemplate>
                      <DataTemplate >
                          <Image Source="{Binding _Source}" />
                      </DataTemplate>
                 </ItemsControl.ItemTemplate>
             </ItemsControl>
         <TextBlock Height="30" FontFamily="Trebuchet MS" FontSize="18" Text="{Binding _Name}" />
      </StackPanel>
  </Border>
</DataTemplate>

一切都绑定好了。这是从用户控件内部调用的。

<ItemsControl ItemTemplate="{StaticResource siteView}" ItemsSource="{Binding Path=_SiteDisplay"/>

我的可观察集合“_SiteDisplay”包含另一个可观察集合“_Collection”,它保存了图像的url。

这是从实际代码中简化出来的,但说明了问题。我无法使图像水平对齐!非常感谢任何帮助或更好的解决方法的建议。

1个回答

12

你需要更改ItemsControl使用的面板,而不是包含ItemsControl的面板:

<ItemsControl ItemsSource="{Binding Path=_Collection, Mode=OneWay}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate >
            <Image Source="{Binding _Source}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate >
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

1
非常感谢 - 这么简单但是它让我抓狂了! - Jas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接