首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Silverlight ItemsPanel - WrapPanel

Silverlight ItemsPanel - WrapPanel
EN

Stack Overflow用户
提问于 2010-10-22 04:44:28
回答 2查看 3.2K关注 0票数 1
代码语言:javascript
复制
    <ItemsPanelTemplate x:Key="lbWrapPanelItemsPanelTemplate">
        <wp:WrapPanel Margin="2" Background="Beige" HorizontalAlignment="Stretch">
        </wp:WrapPanel>
    </ItemsPanelTemplate>

.

代码语言:javascript
复制
<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>

除了(米色的) WrapPanel似乎延伸到无限之外,ListBox运行得很好。当更多的项目被添加到ListBox中时,它只会向右滚动,而不是换行。如果我向WrapPanel添加一个具体的大小,就会导致换行,但(显然)会导致LB中的项显示在所有可用空间的子集中。

有没有办法告诉WrapPanel占用所有可用空间而不是更多?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-22 04:46:56

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

您可以影响WrapPanel中的ScrollViewer并强制其包装:

代码语言:javascript
复制
<ListBox HorizontalAlignment="Stretch" ItemsSource="{Binding foo}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <tk:WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

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

票数 4
EN

Stack Overflow用户

发布于 2010-10-22 05:40:49

您可以通过将WrapPanel的宽度绑定到ListBoxActualWidth来修复此问题

代码语言:javascript
复制
<WrapPanel Width="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=ActualWidth"}

(当然,除非它只适用于WPF,而不适用于Silverlight,我不知道这一点。)

奇怪的是,为什么ListBox可以做到这一点,而ItemsControl却不能做到这一点。在下面的页面中,WrapPanel的页边距正确,其中包含的项也正确换行:

代码语言:javascript
复制
<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,但它不起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3991759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档