嵌套ItemsControl的拉伸高度

时间:2013-08-18 18:20:27

标签: c# wpf height itemscontrol

在MainWindow上,我有一个ItemsControl我正在添加UserControls,并且UserControl还包含一个ItemsControl,其中包含其他UserControls

问题是第二个ItemsControl中的很多项目没有显示,因为它们超出了MainWindow上显示的UserControl的高度。

如何确保第一个ItemsControl中的UserControl始终足够伸展以显示其ItemsControl中的所有UserControl?对于试验和错误,我尝试通过ItemTemplate伸展,但这并没有改变一件事。

<Window x:Class="X.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="DataTemplate1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>    
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <ItemsPresenter Grid.Row="0" Grid.Column="0" DataContext="{Binding}"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ItemsControl Grid.Row="0" x:Name="ic" ItemTemplate="{DynamicResource DataTemplate1}"/>
    </Grid>
</Window>

-

<UserControl x:Class="X.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="113" Width="209">
    <UserControl.Resources>
        <DataTemplate x:Key="DataTemplate1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <ItemsPresenter DataContext="{Binding}"/>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <ItemsControl Grid.Row="1" x:Name="ic" Margin="0,36,0,0" ItemTemplate="{DynamicResource DataTemplate1}"/>
    </Grid>
</UserControl>

-

<UserControl x:Class="X.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="40" Width="64">
    <Grid>
        <Label Content="Label2" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>

    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

从我上面的评论中添加,结果证明是问题。

您的UserControl明确设置了HeightWidth,这意味着他们不会自动调整大小/拉伸以适应您想要的子控件。

删除Height(理想情况下为Width)应该可以解决问题:

<UserControl x:Class="X.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" />
    <!-- Your Code -->         
</UserControl>