【问题标题】:Stretch WrapGrid item width拉伸 WrapGrid 项目宽度
【发布时间】:2012-11-07 07:27:24
【问题描述】:

我在ListView 中使用WrapGrid 作为itemscontrol 以使用以下XAML 代码(WinRT) 水平显示项目

<ItemsPanelTemplate >
    <WrapGrid x:Name="ContentGrid" MinWidth="200" MinHeight="0" VerticalAlignment="Top"   HorizontalAlignment="Center"   Orientation="Horizontal" Margin="0,0,0,5" >                                     
    </WrapGrid>
</ItemsPanelTemplate>

现在当宽度大于MinWidth 并且不能水平添加更多项目时,我如何水平拉伸项目。 (所有项目都居中对齐,水平两侧有很多空间)

<DataTemplate x:Key="CustomChildItemTemplete">
   <Grid Background="Red">
   </Grid>
</DataTemplate >

更多详细信息:listview 宽度约为 800(大约)时,它会水平显示 4 个项目(因为最小项目宽度为 200),但如果宽度为 900(可见 4 个项目)并且空格(50 px)在itemscontrol 的左右两边,我如何通过增加项目宽度来删除这个空格(当listview 宽度为 900 时,项目宽度必须为 225)

【问题讨论】:

  • 我不太了解你的问题。也许您的结果的屏幕截图可能有用?
  • 这是动态的(你的 ItemsControl 的大小发生变化,还是由布局自动设置),还是你自己设置宽度或 itemsControl?
  • @NicolasVoron,由布局自动设置

标签: c# xaml windows-8 microsoft-metro windows-runtime


【解决方案1】:

最简单的方法是像这样绑定MinWidth

<ItemsControl x:Name="MyItemsControl">
  <ItemsPanelTemplate >
      <WrapGrid x:Name="ContentGrid" MinWidth="{Binding Path=Width,  MinWidth="{Binding RelativeSource={RelativeSource Mode=Self}, Converter={StaticRessource Myconverter}, ConverterParameter=[Here Nb of object that you want in one line]}" MinHeight="0" VerticalAlignment="Top"   HorizontalAlignment="Center"   Orientation="Horizontal" Margin="0,0,0,5" >                                     
      </WrapGrid>
  </ItemsPanelTemplate>
</ItemsControl>

转换器只需将 itemsControl 的宽度除以参数(一行中您想要的对象的 Nb)。所以对象会自动达到您想要的大小!

【讨论】:

  • 我不测试这段代码,所以它可能不能“按原样”工作,但想法是这样的。您可以尝试使用RelativeSource={RelativeSource TemplatedParent},它适用于 wpf,但未使用 winRT 进行测试
  • RelativeSource={RelativeSource Mode=TemplatedParent}} 似乎适用于 winRT
  • 对不起,我不明白:/
  • 使用 MinWidth="{Binding RelativeSource={RelativeSource Mode=Self} 修改代码后工作,如果您可以使用此代码进行编辑,我可以接受您的回答,非常感谢您
  • 如果包含对象的原始宽度设置为"Auto",是否有人知道如何在 UWP 中执行相同操作?
猜你喜欢
  • 1970-01-01
  • 2017-12-14
  • 2021-05-09
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2014-08-15
  • 2014-01-22
  • 2018-04-25
相关资源
最近更新 更多