简体   繁体   English

添加内容时C#WPF网格大小调整

[英]C# WPF Grid Resize When Adding Content

I have a grid inside a scrollviewer that I want to re-size as content is added. 我在scrollviewer内有一个网格,要添加内容时要重新调整大小。 Currently it doesn't resize if I add a button to it out of view. 目前,如果我在视图外添加按钮,它不会调整大小。 Which is odd... but not my main concern. 这很奇怪...但不是我的主要关注点。 My main concern is if I add elements into the grid programmaticly would it resize. 我主要关心的是,如果我以编程方式将元素添加到网格中,它将调整大小。 If not then what should I use to make it resize. 如果没有,那么我应该使用什么来调整它的大小。

Here is the current idea 这是当前的想法

<DockPanel HorizontalAlignment="Left" Height="266" LastChildFill="False" VerticalAlignment="Top" Width="434">
    <Menu Height="20" VerticalAlignment="Top" DockPanel.Dock="Top">
        <MenuItem Header="MenuItem">
            <MenuItem Header="MenuItem" HorizontalAlignment="Left" Width="145"/>
        </MenuItem>
    </Menu>
    <ScrollViewer DockPanel.Dock="Bottom" Height="246" RenderTransformOrigin="0.5,0.5">
        <Grid Height="173" Width="100"/>

    </ScrollViewer>
</DockPanel>

Hi try to remove Width="434" from your DockPanel and other widths from you control Width="100", if width is fixed than you cant resize you should remove width property, i hope that works 嗨,尝试从您的DockPanel中删除Width =“ 434”,从您控制Width =“ 100”中删除其他宽度,如果宽度固定,无法调整大小,则应该删除width属性,希望如此

EDIT: You can use StackPanel enstead Grid, your problem was when you addin some element it is adding top of another element. 编辑:您可以使用StackPanel代替网格,您的问题是当您添加某些元素时它正在添加另一个元素的顶部。 xaml code: XAML代码:

 <DockPanel HorizontalAlignment="Left" VerticalAlignment="Top"  >
    <Menu Height="20" VerticalAlignment="Top" DockPanel.Dock="Top">
        <MenuItem Header="MenuItem">
            <MenuItem Header="MenuItem" HorizontalAlignment="Left" Width="145"/>
        </MenuItem>
    </Menu>
    <ScrollViewer DockPanel.Dock="Bottom" >
        <StackPanel Name="uiStack" Orientation="Vertical">


        </StackPanel>
    </ScrollViewer>
    <Button Content="Click me" Click="Button_Click"></Button>
</DockPanel>

codeBehind: 代码背后:

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        uiStack.Children.Add(new Button() { Content = "Button1" });
        uiStack.Children.Add(new TextBlock() { Text = "Button1" }); 
    }

The way you try to use the Grid is not the way it works. 您尝试使用网格的方法不是它的工作方式。 To have it working you have to define at least either row or column. 要使其工作,您必须至少定义行或列。 Then you can place your controls in specific rows and columns. 然后,您可以将控件放置在特定的行和列中。 It looks like in your case you only need single row and column. 看起来在您的情况下,您只需要单个行和一列。 It is in the row/column definition where you define the size and whether it should resize or not. 在行/列定义中,您可以定义大小以及是否应调整大小。

<ScrollViewer DockPanel.Dock="Bottom" Height="246" RenderTransformOrigin="0.5,0.5">
    <Grid>
        <!--Define rows and columns-->
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>            
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <!--Place a control in the grid-->
        <Button Grid.Row="0" Grid.Column="0" Width="100" Height="100"/>
    </Grid>
</ScrollViewer>

The Height and Width properties are set to Auto . HeightWidth属性设置为Auto It means they will readjust themselves to the size of the content. 这意味着他们将根据内容的大小进行调整。 For instance, in the example above the button's height and width are both set to 100. It means the row and the column will maintain these values. 例如,在上面的示例中,按钮的高度和宽度都设置为100。这意味着行和列将保留这些值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM