C# Can';无法使WPF Items控件垂直拉伸

C# Can';无法使WPF Items控件垂直拉伸,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我最近开始使用MVVM开发一个小型WPF项目 然而,我遇到了一些麻烦,让我的ItemsControl横扫整个主窗口。它在水平方向上按预期工作,但在垂直方向上什么也不做 当我在ItemControl视图中更改DesignHeight时,它会像预期的那样拉伸,因此我很容易相信问题出现在主窗口中 主窗口的XAML: <Window x:Class="PhoneDirectoryEditor.MainWindow" xmlns="http://schemas.microsoft.c

我最近开始使用MVVM开发一个小型WPF项目

然而,我遇到了一些麻烦,让我的ItemsControl横扫整个主窗口。它在水平方向上按预期工作,但在垂直方向上什么也不做

当我在ItemControl视图中更改DesignHeight时,它会像预期的那样拉伸,因此我很容易相信问题出现在主窗口中

主窗口的XAML:

<Window x:Class="PhoneDirectoryEditor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PhoneDirectoryEditor"
        xmlns:vm="clr-namespace:PhoneDirectoryEditor.ViewModel"
        xmlns:vw="clr-namespace:PhoneDirectoryEditor.View"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="660" d:DataContext="{d:DesignInstance vm:MainWindowViewModel}" MinHeight="400" MinWidth="400">
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:DirectoryEntryListViewModel}">
            <vw:DirectoryEntryListView/>
        </DataTemplate>
    </Window.Resources>
    <Grid Margin="4">
        <ItemsControl ItemsSource="{Binding ViewModels}"/>
    </Grid>
</Window>
<UserControl x:Class="PhoneDirectoryEditor.View.DirectoryEntryListView"
             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" 
             xmlns:local="clr-namespace:PhoneDirectoryEditor.View"
             xmlns:viewModel="clr-namespace:PhoneDirectoryEditor.ViewModel"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance viewModel:DirectoryEntryListViewModel}" d:DesignWidth="512" d:DesignHeight="383">

    <UserControl.Resources>
        <!--body content datagrid cell vertical centering-->
        <Style x:Key="BodyContentDataGridCentering" TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Center" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid Background="GhostWhite">
        <DataGrid Grid.Row="0" Grid.Column="0" ItemsSource="{Binding AllDirectoryEntries}" CanUserReorderColumns="False" CanUserResizeRows="False" SelectionMode="Single" AutoGenerateColumns="False" RowDetailsVisibilityMode="Collapsed" HeadersVisibility="Column" GridLinesVisibility="Horizontal" CanUserAddRows="False" Margin="3,3,3,46" MaxHeight="400">
            <DataGrid.Columns>
                <DataGridTextColumn CellStyle="{StaticResource BodyContentDataGridCentering}" Binding="{Binding Name}" ClipboardContentBinding="{x:Null}" Header="Name" Width="*"/>
                <DataGridTextColumn CellStyle="{StaticResource BodyContentDataGridCentering}" Binding="{Binding PhoneNumber}" ClipboardContentBinding="{x:Null}" Header="Phone number" Width="*"/>
                <DataGridTemplateColumn Header="Delete Contact" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Delete" Tag="{Binding}" Margin="2"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Button Content="Search" Tag="{Binding}" Margin="150,0,0,9" HorizontalAlignment="Left" Width="87" Height="23" VerticalAlignment="Bottom"/>
        <TextBox Grid.Column="0" x:Name="textBox" TextWrapping="Wrap" Text="TextBox" Margin="3,0,0,9" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="140"/>
        <Button Content="New" Tag="{Binding}" Margin="0,0,3,9" HorizontalAlignment="Right" Width="104" Height="23" VerticalAlignment="Bottom"/>

    </Grid>
</UserControl>

ItemControl的XAML:

<Window x:Class="PhoneDirectoryEditor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PhoneDirectoryEditor"
        xmlns:vm="clr-namespace:PhoneDirectoryEditor.ViewModel"
        xmlns:vw="clr-namespace:PhoneDirectoryEditor.View"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="660" d:DataContext="{d:DesignInstance vm:MainWindowViewModel}" MinHeight="400" MinWidth="400">
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:DirectoryEntryListViewModel}">
            <vw:DirectoryEntryListView/>
        </DataTemplate>
    </Window.Resources>
    <Grid Margin="4">
        <ItemsControl ItemsSource="{Binding ViewModels}"/>
    </Grid>
</Window>
<UserControl x:Class="PhoneDirectoryEditor.View.DirectoryEntryListView"
             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" 
             xmlns:local="clr-namespace:PhoneDirectoryEditor.View"
             xmlns:viewModel="clr-namespace:PhoneDirectoryEditor.ViewModel"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance viewModel:DirectoryEntryListViewModel}" d:DesignWidth="512" d:DesignHeight="383">

    <UserControl.Resources>
        <!--body content datagrid cell vertical centering-->
        <Style x:Key="BodyContentDataGridCentering" TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Center" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid Background="GhostWhite">
        <DataGrid Grid.Row="0" Grid.Column="0" ItemsSource="{Binding AllDirectoryEntries}" CanUserReorderColumns="False" CanUserResizeRows="False" SelectionMode="Single" AutoGenerateColumns="False" RowDetailsVisibilityMode="Collapsed" HeadersVisibility="Column" GridLinesVisibility="Horizontal" CanUserAddRows="False" Margin="3,3,3,46" MaxHeight="400">
            <DataGrid.Columns>
                <DataGridTextColumn CellStyle="{StaticResource BodyContentDataGridCentering}" Binding="{Binding Name}" ClipboardContentBinding="{x:Null}" Header="Name" Width="*"/>
                <DataGridTextColumn CellStyle="{StaticResource BodyContentDataGridCentering}" Binding="{Binding PhoneNumber}" ClipboardContentBinding="{x:Null}" Header="Phone number" Width="*"/>
                <DataGridTemplateColumn Header="Delete Contact" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Delete" Tag="{Binding}" Margin="2"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Button Content="Search" Tag="{Binding}" Margin="150,0,0,9" HorizontalAlignment="Left" Width="87" Height="23" VerticalAlignment="Bottom"/>
        <TextBox Grid.Column="0" x:Name="textBox" TextWrapping="Wrap" Text="TextBox" Margin="3,0,0,9" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="140"/>
        <Button Content="New" Tag="{Binding}" Margin="0,0,3,9" HorizontalAlignment="Right" Width="104" Height="23" VerticalAlignment="Bottom"/>

    </Grid>
</UserControl>


避免混淆的图像

项控件
上,可以将
垂直内容对齐
指定为
拉伸

<ItemsControl VerticalContentAlignment="Stretch" ...

ItemsControl
上,可以将
VerticalContentAlignment
指定为
Stretch

<ItemsControl VerticalContentAlignment="Stretch" ...

将DataGrid和这两个按钮及其文本框放在一个DockPanel中,其LastChildFill=true

根据前面提到的属性,您的DataGrid应该作为DockPanel的最后一个子级添加,因此将这3个控件添加到方向为水平的StackPanel中,然后将DataGrid放在后面

下面是一个如何工作的示例:

<Window x:Class="GridHeightStack.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<DockPanel LastChildFill="True">
    <DockPanel.Resources>
        <x:Array Type="system:String" x:Key="days">
            <system:String>Item1</system:String>
            <system:String>Item2</system:String>
            <system:String>Item3</system:String>
            <system:String>Item4</system:String>
            <system:String>Item5</system:String>
            <system:String>Item6</system:String>
        </x:Array>
    </DockPanel.Resources>
    <DockPanel  DockPanel.Dock="Bottom" Height="30">
        <TextBox Width="100" Text="test text" />
        <Button  Content="Search"/>
        <Button  Content="New" HorizontalAlignment="Right"/>
    </DockPanel>
    <Border BorderBrush="Brown" BorderThickness="2">
        <DataGrid ItemsSource="{StaticResource days}" FontSize="30" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding}"/>
            </DataGrid.Columns>

        </DataGrid>
    </Border>
</DockPanel>

项目1
项目2
项目3
项目4
项目5
项目6

结果是:


我给DataGrid的边框涂上了颜色,这样您就可以看到它展开了,并且底部的项目保留在它们的位置上。

将您的DataGrid和这两个按钮以及它们的文本框放在一个DockPanel中,其LastChildFill=true

根据前面提到的属性,您的DataGrid应该作为DockPanel的最后一个子级添加,因此将这3个控件添加到方向为水平的StackPanel中,然后将DataGrid放在后面

下面是一个如何工作的示例:

<Window x:Class="GridHeightStack.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<DockPanel LastChildFill="True">
    <DockPanel.Resources>
        <x:Array Type="system:String" x:Key="days">
            <system:String>Item1</system:String>
            <system:String>Item2</system:String>
            <system:String>Item3</system:String>
            <system:String>Item4</system:String>
            <system:String>Item5</system:String>
            <system:String>Item6</system:String>
        </x:Array>
    </DockPanel.Resources>
    <DockPanel  DockPanel.Dock="Bottom" Height="30">
        <TextBox Width="100" Text="test text" />
        <Button  Content="Search"/>
        <Button  Content="New" HorizontalAlignment="Right"/>
    </DockPanel>
    <Border BorderBrush="Brown" BorderThickness="2">
        <DataGrid ItemsSource="{StaticResource days}" FontSize="30" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding}"/>
            </DataGrid.Columns>

        </DataGrid>
    </Border>
</DockPanel>

项目1
项目2
项目3
项目4
项目5
项目6

结果是:


我给DataGrid的边框涂上了颜色,这样您就可以看到它展开了,并且底部的项目仍保留在它们的位置上。

我去掉了DataGrid上的最大高度
MaxHeight=“400”



它对我很有效

我去掉了数据网格上的最大高度
MaxHeight=“400”



它对我很有效

我把最大高度放进去,以确保我得到一个滚动条。我把它取下来,按钮被向下移动,悲伤地把MaxHeight放在窗外,以确保我有一个滚动条。我把它取下来,按钮被向下移动,离开了窗口,很难过,谢谢你提供了详细的信息,我试过了,但它仍然只是水平延伸。对于向下的投票者:你试过我的例子吗?它尽可能简单,而且工作做得很完美。@Robert去掉数据网格上的MaxHeight,只需在DockPanel上添加高度,在我的示例中,我取了Height=“30”。我刚刚做了一个更新来证明显而易见的事实。祝你好运谢谢你提供的详细信息,我试过了,但它仍然只适用于下层选民:你试过我的例子吗?它尽可能简单,而且工作做得很完美。@Robert去掉数据网格上的MaxHeight,只需在DockPanel上添加高度,在我的示例中,我取了Height=“30”。我刚刚做了一个更新来证明显而易见的事实。祝你好运