简体   繁体   English

WPF网格 - 如何在调整窗口大小时保持列宽?

[英]WPF Grids - how to maintain column width when window is resized?

I have this XAML markup... 我有这个XAML标记......

<Grid Name="ProductsGrid" Width="500">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="ProductList" Width="*" />
            <ColumnDefinition Name="ProductInfo" Width="100" MinWidth="10" MaxWidth="100"/>
        </Grid.ColumnDefinitions>

When the app starts the 2nd column is se to 100 "units" wide. 当应用程序启动时,第二列的宽度为100“单位”。

When the window is resized the column grows and shrinks - it maintains its ratio with column 1 which I think is what i supposed to happen in WPF. 当窗口调整大小时,列会增长和缩小 - 它保持与第1列的比率,我认为这是我应该在WPF中发生的事情。 So when the app starts the window size is set to 500 and the 2nd column is 1/5 of the total width. 因此,当应用程序启动时,窗口大小设置为500,第二列是总宽度的1/5。 As the app resizes it maintains 1/5 of the total width However in this example I want it to stay at 100 units. 随着应用程序调整大小,它保持总宽度的1/5。但在此示例中,我希望它保持在100个单位。

Any ideas? 有任何想法吗?

Remove the width of the grid, it is fixing the width of the grid, the grid remains 100 in width ... give the grid background color if you want to see the actual grid location. 删除网格的宽度,它固定网格的宽度,网格保持宽度为100 ...如果要查看实际网格位置,请给出网格背景颜色。

you can fix the initial width of the main window to 100 and leave the grid without fixed width to allow the desired behavior 您可以将主窗口的初始宽度固定为100,并使网格没有固定宽度,以允许所需的行为

try this: 尝试这个:

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Width="100" x:Name="MyWindow">
    <Grid Name="ProductsGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Name="ProductList" Width="*" />
                <ColumnDefinition Name="ProductInfo" Width="100" MinWidth="10" MaxWidth="100"/>
            </Grid.ColumnDefinitions>

Try this 尝试这个

<Grid Name="ProductsGrid"  ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="ProductList" Width="*" />
            <ColumnDefinition Name="ProductInfo" Width="100" MinWidth="10" MaxWidth="100"/>
        </Grid.ColumnDefinitions>
    </Grid>

You need to remove the fixed width of Grid. 您需要删除Grid的固定宽度。

If you want your initial window to be of size 500 then you can handle window loaded event and set the size of window to 500. Don't hard code size of Grid in code behind or XAML otherwise you will face same issue again 如果您希望初始窗口大小为500,那么您可以处理window loaded event并将window大小设置为500.不要在代码后面或XAML中硬编码Grid的大小,否则您将再次面临同样的问题

 private void mywindow_Loaded(object sender, RoutedEventArgs e)
            {
                this.Width = 500;
            }

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

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