c# - 当 StackPanel 宽度为全宽时,WP8 ListBox DataTemplate 抛出异常

标签 c# windows-phone-8

具体的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致力于GridColumnDefinition ,它不是 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 值。

如果这些建议均无效,则问题出在 ListBoxItemListBox 默认模板内。

关于c# - 当 StackPanel 宽度为全宽时,WP8 ListBox DataTemplate 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19547373/

相关文章:

c# - 将自定义对象添加到 C# 中的选中列表框

c# - 如何在 VS 2013 中调试 Windows Phone (8) 运行时组件或 C++ DLL 中的 C++ 代码

c# - Windows Phone 8.1 - 页面导航

c# - 并行 Foreach 超时优雅地关闭每个工作线程

c# - Windows Phone 8 倾斜效果不适用于网格

windows 8 手机开发可以在 windows 7 下进行吗?

windows-phone-7 - Windows Phone ApplicationBar MenuItems空间问题

c# - 使用 C# 从 Google Chrome 获取当前选项卡的 URL

c# - ASP.NET 表单值验证场景

c# - 为什么我用这个不同步的代码反复得到相同的结果?