WPF - Usercontrol高度不正确

我有一个我无法弄清楚的问题。我希望我能解释得足够多。 基本上,我有一个usercontrol,我希望在窗口模式对话框中使用它。
<Grid>
    <Rectangle Opacity=".75" Fill="White"/>
    <Border Width="425" BorderBrush="LightGray" BorderThickness="2" CornerRadius="20,0,20,0" Padding="3">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="15"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
            <!-- First Name -->
            <Label Grid.Row="0" Grid.Column="0" Content="First Name:"/>
            <TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding FirstName}" Margin="3"/>
            <!-- Last Name -->
            <Label Grid.Row="0" Grid.Column="3" Content="Last Name:"/>
            <TextBox Grid.Row="0" Grid.Column="4" Grid.ColumnSpan="2" Text="{Binding LastName}" Margin="3"/>
            <!-- Address -->
            <Label Grid.Row="1" Grid.Column="0" Content="Address:"/>
            <TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" Text="{Binding Address}" HorizontalAlignment="Stretch" Margin="3"/>
            <!-- City -->
            <Label Grid.Row="2" Grid.Column="0" Content="City:"/>
            <TextBox Grid.Row="2" Grid.Column="1"  Text="{Binding City}" Margin="3"/>
            <!-- State -->
            <Label Grid.Row="2" Grid.Column="2" Content="State:"/>
            <ComboBox Grid.Row="2" Grid.Column="3" ItemsSource="{Binding States}" SelectedValue="{Binding State}" Margin="3"/>
            <!-- Zip Code -->
            <Label Grid.Row="2" Grid.Column="4" Content="Zip Code:"/>
            <TextBox Grid.Row="2" Grid.Column="5" Text="{Binding ZipCode}" Margin="3"/>
            <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="6" Width="100" HorizontalAlignment="Center" Content="Save" Command="{Binding SaveCustomerCommand}"/>
        </Grid>
    </Border>
</Grid>
我还有一个包含datatemplate的resourcedictionary,用于将此usercontrol连接到它的viewmodel。
<DataTemplate DataType="{x:Type vm:CreateCustomerViewModel}">
    <view:CreateCustomerView/>
</DataTemplate>
最后,在主窗口viewmodel中,我创建了一个控件的viewmodel实例,在主窗口视图中,我使用了itemscontrol并将它的itemssource属性绑定到控件的viewmodel实例。
        <ItemsControl Height="600" Grid.Row="0" ItemsSource="{Binding CreateCustomerViewModel}" Grid.RowSpan="2" />
现在,我的问题是在主窗口中使用itemscontrol,我尝试了几种不同的方法,但我无法将控件作为窗口的高度。我不确定我是不是应该使用物品控制,或者我做错了什么。很感谢任何形式的帮助。     
已邀请:
ItemsControl用于收藏。默认情况下,它使用StackPanel来包含其子元素,这不允许在堆栈方向上进行拉伸(默认情况下为Vertical)。对于单个项目,请使用ContentControl(Button和Label之类的基础):
<ContentControl Height="600" Grid.Row="0" Content="{Binding CreateCustomerViewModel}" Grid.RowSpan="2" />
    
您可以尝试以下方法: 把你的
ItemsControl
放在
Grid
。 用
VerticalContentAlignment="Stretch"
声明你的
ItemsControl
。 它应该没有任何区别,因为它是默认设置,但是用
VerticalAlignment="Stretch"
声明你的
ItemsControl
并尝试删除
Height="600"
。     

要回复问题请先登录注册