简体   繁体   English

如何在拉伸其父窗口时垂直拉伸WPF用户控件及其组件

[英]How can I vertically stretch a WPF user control and its components while stretching its parent window

I have a user control with a text box and 2 buttons below it. 我有一个带有文本框和下面两个按钮的用户控件。 I want the text box to stretch vertically when the parent window stretches vertically but nothing happens. 当父窗口垂直拉伸但什么也没发生时,我希望文本框垂直拉伸。 Another problem is that the buttons appear bunched up under the text box when the user control is put into a window. 另一个问题是,当用户控件置于窗口中时,按钮在文本框下显得很乱。 But when I view the user control while not in a window the buttons are separated by 1 grid row. 但是,当我不在窗口中查看用户控件时,按钮之间用1个网格行隔开。 How can I get this to work properly so the text box increases in size when the parent window is stretched vertically and also have the buttons at the bottom stay away from the text box? 如何使其正常工作,以便在垂直拉伸父窗口时使文本框增大,并使底部的按钮远离文本框?

Here is the xaml code for the window that uses the user control: 这是使用用户控件的窗口的xaml代码:

<Window x:Class="SerialNumTool.Views.test2View"
        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:SerialNumTool.UserControls"
        mc:Ignorable="d"
        Title="test2View" Height="300" Width="300"
        VerticalAlignment="Stretch">
    <Grid Margin="0,0,0,0"  Background="Cyan" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".5*"/>
            <ColumnDefinition Width=".5*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>

        </Grid.RowDefinitions>
        <StackPanel Name="MainContentAreaStackPanel" Margin="0" VerticalAlignment="Stretch" 
                    HorizontalAlignment="Stretch"
                    Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" Grid.ColumnSpan="2">
            <local:UserControl2  VerticalAlignment="Stretch" 
                                 HorizontalAlignment="Stretch">
            </local:UserControl2>
        </StackPanel>


    </Grid>
</Window>

Here is the user control code: 这是用户控制代码:

<UserControl x:Class="SerialNumTool.UserControls.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" 
             xmlns:local="clr-namespace:SerialNumTool.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="200"
             VerticalAlignment="Stretch">
    <Grid Margin="0,0,0,0"  Background="Beige" >
        <Grid.ColumnDefinitions>
                <ColumnDefinition Width=".5*"/>
                <ColumnDefinition Width=".5*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height=".20*"/>
                <RowDefinition Height=".20*"/>
                <RowDefinition Height=".20*"/>
                <RowDefinition Height=".20*"/>
                <RowDefinition Height=".20*"/>
        </Grid.RowDefinitions>
        <StackPanel Background="Green" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="1" Grid.RowSpan="2">
        <TextBox x:Name="TextOutputAreaTextBox" 
                      HorizontalAlignment="Stretch" Margin="5"  
                      VerticalAlignment="Stretch" AcceptsReturn="True"
                      VerticalScrollBarVisibility="Auto"
                      TextWrapping="Wrap" />

        </StackPanel>
        <StackPanel Orientation="Horizontal" Background="Green" Grid.Column="0" Grid.ColumnSpan="2" 
                    Margin="0,0,0,0" Grid.Row="4" Grid.RowSpan="1" HorizontalAlignment="Stretch" >

            <Button Content="Operation 2" Margin="5"></Button>
            <Button Content="Operation 3" Margin="5"></Button>
        </StackPanel>

    </Grid>
</UserControl>

Thanks in advance. 提前致谢。

You can greatly simplify your markup by using just grids. 仅使用网格就可以大大简化标记。

You seem to have misunderstood the way the * gridmeasure works. 您似乎误解了* gridmeasure的工作方式。

I think the markup below does what you're trying to achieve. 我认为下面的标记可以实现您想要的目标。 When you put most controls in a grid(cell) they will expand to take up all the space available. 当您将大多数控件放在网格(单元)中时,它们将扩展以占据所有可用空间。 That's what you want here so all you want is grids and cells. 这就是您想要的,因此您只需要网格和单元格。

The * is not an abstracted measure of the actual height in the way you are using it. *不是您使用它时实际高度的抽象度量。 You had a control spanning two rows. 您有一个跨两行的控件。 I simplified that by making one of the rows 2* height. 我通过将行之一设为2 *高度来简化了这一过程。 If you wanted two controls in the right column next to it then you would of course probably want 5 rows. 如果您想要在其旁边的右列中包含两个控件,那么您当然可能需要5行。 But you don't have that. 但是你没有。

    xmlns:local="clr-namespace:wpf_99"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    >
    <Grid Background="Cyan" >
        <local:UserControl2/>
    </Grid>
</Window>

and

         d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="Beige" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox x:Name="TextOutputAreaTextBox" 
                HorizontalAlignment="Stretch" 
                Margin="5"  
                VerticalAlignment="Stretch" 
                AcceptsReturn="True"
                VerticalScrollBarVisibility="Auto"
                TextWrapping="Wrap" />

        <Button Content="Operation 2" Margin="5" Grid.Row="4"></Button>
        <Button Content="Operation 3" Margin="5" Grid.Row="4" Grid.Column="1"></Button>
    </Grid>
</UserControl>

Henk Holterman's comment to remove the StackPanel around the text solves the problem. Henk Holterman的评论删除了文本周围的StackPanel,从而解决了该问题。 The textbox would not vertically stretch in or outside a user control while inside a StackPanel. 在StackPanel中时,该文本框不会在用户控件的内部或外部垂直延伸。 I had to remove the StackPanel around the textbox in the user control as well as in the window that used the user control. 我必须删除用户控件以及使用该用户控件的窗口中文本框周围的StackPanel。 Below are the code updates for a working sample: 下面是一个工作示例的代码更新:

The User Control with StackPanel removed: 删除了StackPanel的用户控件:

<UserControl x:Class="SerialNumTool.UserControls.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" 
             xmlns:local="clr-namespace:SerialNumTool.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="200"
             VerticalAlignment="Stretch">
    <Grid Margin="0,0,0,0"  Background="Beige" >
        <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="TextOutputAreaTextBox" Grid.Column="0" Grid.Row="1" 
                      HorizontalAlignment="Stretch" Margin="5"  
                      VerticalAlignment="Stretch"  VerticalContentAlignment="Stretch"
                      AcceptsReturn="True"
                      VerticalScrollBarVisibility="Auto"
                      TextWrapping="Wrap" />
        <StackPanel Orientation="Horizontal" Background="Green" Grid.Column="0" Grid.ColumnSpan="2" 
                    Margin="0,0,0,0" Grid.Row="4" Grid.RowSpan="1" HorizontalAlignment="Stretch" >

            <Button Content="Operation 2" Margin="5"></Button>
            <Button Content="Operation 3" Margin="5"></Button>
        </StackPanel>
    </Grid>
</UserControl>

Here is the window that used the user control: 这是使用用户控件的窗口:

<Window x:Class="SerialNumTool.Views.test2View"
        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:SerialNumTool.UserControls"
        mc:Ignorable="d"
        Title="test2View" Height="300" Width="300"
        VerticalAlignment="Stretch">
    <Grid Margin="0,0,0,0"  Background="Cyan" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".5*"/>
            <ColumnDefinition Width=".5*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>
            <RowDefinition Height=".20*"/>

        </Grid.RowDefinitions>
        <local:UserControl2  VerticalAlignment="Stretch" 
                             HorizontalAlignment="Stretch"
                             Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" Grid.ColumnSpan="2">
        </local:UserControl2>
    </Grid>
</Window>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将WPF用户控件的宽度拉伸到其窗口? - How to stretch in width a WPF user control to its window? 如何将字符串从用户控件传递到WPF中的父窗口? - How to pass a string from user control to its parent Window in WPF? 拖动用户控件,但将其保留在WPF中其父级的边界内 - Drag User Control, but keep it inside the bounds of its parent in WPF WPF 控件是否可以以其父控件为中心,但也可以仅使用 xaml 尊重其兄弟控件的 position? - Can a WPF control be centered around its parent but also respect the position of its sibling control using xaml only? 我怎样才能获得用户控件wpf的父级 - how can i get the parent of user control wpf 如何与WPF用户控件中托管的Delphi窗口通信? - How can I communicate with a Delphi window hosted in a WPF User Control? 如何使网格在用户拉伸范围内更改其高度和背景图片? - How do I make the grid to change its height and its background picture within user`s stretching? ReactiveUI + WPF:ViewModelViewHost 不会在其父级内部拉伸 - ReactiveUI + WPF: ViewModelViewHost does not stretch inside its parent System.Windows.Controls.WebBrowser(IFramed 窗口)如何在 xaml(WPF) 中与其父视图通信 - How does System.Windows.Controls.WebBrowser (IFramed window) can communicate to its parent view in xaml(WPF) 如何在C#中使文本框拉伸到其父窗口的宽度? - How to make a textbox stretch to its parent window's width in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM