xaml / c#stackpanel与背景矩形

时间:2013-05-22 19:56:25

标签: c# xaml windows-phone-7.1 windows-phone

我使用基于StackPanel的元素模板在XAML中有一个列表(ListBox)。布局很好,但我现在想要一个矩形作为每个项目的背景 - 围绕每个项目创建一个框。

我正在考虑以某种方式使用Canvas,但由于每个项目的高度不同(以及StackPanel内部项目的高度),我不知道该怎么做(我是C#/ XAML的新手) )。在这种情况下,模板的最佳组合是什么?

2 个答案:

答案 0 :(得分:3)

你可以在ItemTemplate中指定它,它会做你想做的事情,比如;

<ListBox.ItemTemplate>
  <DataTemplate>

    <Grid>
       <Border BorderBrush="Red" BorderThickness="2" Background="Blue"/>
       <!-- Insert the rest of your Item template stuff here -->
    </Grid>

  </DataTemplate>
</ListBox.ItemTemplate>

答案 1 :(得分:0)

ListBox是ItemsControl的一种类型,它公开了几个属性来控制项目的外观。在这种情况下,看一下ItemContainerStyle(在ListBox的情况下,item容器是ListBoxItem的实例)。例如,您可以将ItemsContainerStyle中的Background属性设置为某种颜色。