【问题标题】:Silverlight ItemsPanel - WrapPanelSilverlight ItemsPanel - WrapPanel
【发布时间】:2010-10-21 20:44:28
【问题描述】:
    <ItemsPanelTemplate x:Key="lbWrapPanelItemsPanelTemplate">
        <wp:WrapPanel Margin="2" Background="Beige" HorizontalAlignment="Stretch">
        </wp:WrapPanel>
    </ItemsPanelTemplate>

.....

<Grid Background="LightCoral" MinWidth="500" MinHeight="500">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <sdk:GridSplitter Grid.Column="0" Background="AliceBlue" />

    <StackPanel Grid.Column="0" FlowDirection="LeftToRight">
        <Button Width="40">Left</Button>
        <Button Width="40">Right</Button>
    </StackPanel>

    <Grid Background="Bisque" Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="100"></RowDefinition>
        </Grid.RowDefinitions>

        <ListBox ItemsPanel="{StaticResource lbWrapPanelItemsPanelTemplate}" x:Name="bookListBox" HorizontalAlignment="Stretch"  Grid.Row="0" ItemsSource="{Binding Path=BookSource}" ItemTemplate="{StaticResource dirtSimple}"  />

        <wp:WrapPanel Grid.Row="1">
            <Button Width="200" Command="{Binding Path=AddItemCommand}">Bottom</Button>
            <Button Width="200" Command="{Binding ChangeTemplateCommand}" CommandParameter="{StaticResource vmDataTemplate}">White</Button>
            <Button Width="200" Command="{Binding ChangeTemplateCommand}" CommandParameter="{StaticResource vmDataTemplate2}">Lavender</Button>
        </wp:WrapPanel>
    </Grid>

</Grid>

ListBox 工作完美,除了(米色)WrapPanel 似乎延伸到无穷大。随着更多项目添加到 ListBox,它只是向右滚动而不是换行。如果我在 WrapPanel 中添加一个具体的大小,这会导致东西被换行,但(显然)会导致 LB 中的项目显示在所有可用空间的子集中。

有没有办法告诉 WrapPanel 只占用所有可用空间?

【问题讨论】:

    标签: silverlight silverlight-toolkit


    【解决方案1】:

    我总是无法让 WrapPanel 在 ListBoxes 内正常工作(可能是模板中与其中的 ScrollViewer 相关的内容)。如果您将代码放入 ItemsControl 而不是 ListBox 中,您会看到它按原样完美运行。

    您可以影响 WrapPanel 内的 ScrollViewer 并强制其换行:

    <ListBox HorizontalAlignment="Stretch" ItemsSource="{Binding foo}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <tk:WrapPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
    

    注意 ListBox 上设置的 ScrollViewer.Horizo​​ntalScrollBarVisibility - 这可以防止水平滚动条,这会强制 WrapPanel 换行。

    【讨论】:

    • 我刚刚添加了Stretch,但没有效果。完整的 XAML 在那里。非常感谢您的帮助。
    • 哈 - 即使我将 Width="300" 添加到 ListBox,它也只会创建一个 300px 宽的微小 LB,仍然以相同的方式滚动:(
    • 我讨厌 TK 的 WrapPanel。这是唯一不适合我的控件。我有办法解决这个问题,但它很难看。再给我一分钟:)
    • 仅供参考 - 再次查看答案。我用一个解决方案更新了它,通过禁用水平滚动条来强制 WrapPanel 换行。
    • 好吧,我会被诅咒的。那行得通。你是人中的神,亚当·希尔斯先生。非常感谢你。
    【解决方案2】:

    您可以通过将WrapPanel 的宽度绑定到ListBoxActualWidth 来解决此问题:

    <WrapPanel Width="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=ActualWidth"}
    

    (当然,除非这仅适用于 WPF 而不是 Silverlight,我不知道。)

    不过,奇怪的是为什么ListBox 会这样做而ItemsControl 不会。 WrapPanel 具有正确的边距,并且它包含的项目正确包装,在此页面中:

    <Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="700"/>
        </Grid.ColumnDefinitions>
        <ItemsControl 
          Background="Azure"
          Margin="5">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <WrapPanel 
                Background="Lavender"
                Margin="10"/>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
          <ListBoxItem>adsk flaskjf lkasjd flaskdjf laskdj</ListBoxItem>
          <ListBoxItem>adsk flaskjf lkasjd flaskdjf laskdj</ListBoxItem>
          <ListBoxItem>adsk flaskjf lkasjd flaskdjf laskdj flaksjf laskjf aslkjf alsjkf lsafdkj </ListBoxItem>
          <ListBoxItem>adsk flaskjf lkasjd flaskdjf laskdj flaksjf laskjf aslkjf alsjkf lsafdkj </ListBoxItem>
          <ListBoxItem>adsk flaskjf lkasjd flaskdjf laskdj flaksjf laskjf aslkjf alsjkf lsafdkj </ListBoxItem>
          <ListBoxItem>adsk flaskjf lkasjd flaskdjf laskdj flaksjf laskjf aslkjf alsjkf lsafdkj </ListBoxItem>      
        </ItemsControl>
      </Grid>
    </Page>
    

    ItemsControl 更改为ListBox 并没有。

    【讨论】:

    • Silverlight 中没有祖先绑定。 “X”在 WPF 中有效,但在 Silverlight 中无效似乎是一个常见的主题......
    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多