如何强制元素列表填充wrappanel上的窗口?

时间:2013-09-19 16:20:36

标签: wpf xaml listview autofill wrappanel

我尝试在窗口上做一个列列表,当我使用*时,我希望得到与Grid相同的结果。

<ListView ItemsSource="{Binding UcColumns}" 
              HorizontalContentAlignment="Stretch"
              VerticalAlignment="Stretch"                  
              ScrollViewer.VerticalScrollBarVisibility="Disabled"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>                    
                <WrapPanel IsItemsHost="True" Margin="0"                               
                           Orientation="Horizontal" Background="WhiteSmoke"
                           >


                </WrapPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

    </ListView>

问题在于Wrappanel的内容宽度和高度取决于其正确的内容,而不取决于窗口大小。

我希望你能帮助我。

THX。

3 个答案:

答案 0 :(得分:0)

试试这个:

<WrapPanel IsItemsHost="True" Margin="0" Orientation="Horizontal" Background="WhiteSmoke"
           Width="{Binding ActualWidth,
                RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}, Mode=FindAncestor}}"

Height="{Binding ActualHeight,
                RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}, Mode=FindAncestor}}">

答案 1 :(得分:0)

我不知道你为什么要在这里使用wrappanel,但是如果你希望你的listviewitem内容扩展到listview宽度,那么你可以把ListView.ItemContainerStyle放在下面:

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>

并将itemtemplate设置为:

<ListBox.ItemTemplate>
     <DataTemplate>
        <Grid HorizontalAlignment="Stretch">
         <local:MyuserControl HorizontalAlignment="Stretch"/>
        </Grid>
      </DataTemplate>
</ListBox.ItemTemplate>

这会拉伸你的物品

答案 2 :(得分:0)

非常感谢帮助我。

我将你的答案和其他一些搜索组合在一起,我将这个问题解决为写下来。

<ListView ItemsSource="{Binding UcColumns}" 
              HorizontalContentAlignment="Stretch"
              VerticalAlignment="Stretch"                  
              ScrollViewer.VerticalScrollBarVisibility="Disabled"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"></StackPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

        <ListView.ItemTemplate>
            <DataTemplate >
                <uc:UC_Column HorizontalAlignment="Stretch" VerticalAlignment="Stretch"                                      
                                  Width="{Binding ActualWidth,
                                        Converter={cv:DivideConverter},
                                        ConverterParameter=3,
                                        RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}, Mode=FindAncestor}}"
                                  Height="{Binding ActualHeight,
                                        RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}, Mode=FindAncestor}}"/>

            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>

再一次:谢谢!