具体的XAML代码如下:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="6,6,6,6"
Background="Gray"
Orientation="Vertical"
HorizontalAlignment="Left"
Width="*">
...
其他宽度值运行完美。
最佳答案
来自 MSDN :
The width of the object, in pixels. The default is Double.NaN. Except for the special Double.NaN value, this value must be equal to or greater than 0. See Remarks for upper bound information
不能以*
为值。
它可以采用的唯一特殊值是 Double.NaN
(代表 Auto 行为):
The default value of Height and Width is not 0; it is Double.NaN. Height and Width support the ability to be an unset "Auto" value. Because Height and Width are double values, Double.NaN is used as a special value to represent this "Auto" behavior. The layout system interprets the "Auto" value to generally mean that the object should be sized to the available size in layout, instead of to a specific pixel value.
也许您正在混合 StackPanel Width属性(property)与one致力于Grid的 ColumnDefinition ,它不是 double
而是 GridLength
类型。
如果您希望您的 StackPanel
占用所有剩余的宽度空间,那么您必须将它放在 Grid
中:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Margin="6,6,6,6"
Background="Gray"
Orientation="Vertical"
HorizontalAlignment="Left">
...
</Grid>
或
您也可以尝试简单地设置 StackPanel.HorizontalAlignement属性设置为 Stretch
值。
如果这些建议均无效,则问题出在 ListBoxItem
或 ListBox
默认模板内。
关于c# - 当 StackPanel 宽度为全宽时,WP8 ListBox DataTemplate 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19547373/