WPF列定义和起始宽度



我在设置网格中的列宽度时遇到了一些问题。我想实现最左边的列在(应用程序的)启动时是200像素宽,但仍然可以调整大小。这是我的代码:

<Grid x:Name="MainGrid" Width="1000" Height="600">
    <Grid x:Name="MainGrid" HorizontalAlignment="Left" Height="600" VerticalAlignment="Top" Width="1000">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="500"/>
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" MinWidth="200"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalScrollBarVisibility="Visible">
            <TextBox Grid.Column="0" x:Name="textBox" Text="Doei, Hoi" MinWidth="200"/>
        </ScrollViewer>
        <GridSplitter Grid.Column="1" x:Name="gridSplitter" HorizontalAlignment="Center" Height="auto" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="5" ResizeDirection="Columns" Background="#FF464444"/>
        <ScrollViewer Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalScrollBarVisibility="Visible">
            <TextBox Grid.Column="2" x:Name="textbox1" Text="Hoi, Doei" MinWidth="200"/>
        </ScrollViewer>
    </Grid>
</Grid>

我的问题是我不知道如何设置列的起始宽度。也许我用错了搜索词,但我找不到任何能解决我问题的

当前最新代码:

<Window x:Class="ServerWPF.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:ServerWPF"
        mc:Ignorable="d"
        Title="Chat Server" ResizeMode="CanMinimize" SizeToContent="WidthAndHeight">
    <Grid x:Name="MainLeft" HorizontalAlignment="Left" Height="600" VerticalAlignment="Top" Width="1000">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="500"/>
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" MinWidth="200"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalScrollBarVisibility="Visible">
            <TextBox Grid.Column="0" x:Name="textBox" Text="Doei, Hoi" MinWidth="200"/>
        </ScrollViewer>
        <GridSplitter Grid.Column="1" x:Name="gridSplitter" HorizontalAlignment="Center" Height="auto" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="5" ResizeDirection="Columns" Background="#FF464444"/>
        <ScrollViewer Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalScrollBarVisibility="Visible">
            <TextBox Grid.Column="2" x:Name="textbox1" Text="Hoi, Doei" MinWidth="200"/>
        </ScrollViewer>
    </Grid>
</Window>

这对我很有效,只需将起始宽度设置为宽度。无需再次将其设置为所有子元素,这些子元素将调整大小以适应其容器。

我在左框上设置了MaxWidth,以防止它将右框中的内容滚动到可见区域之外。

<Grid x:Name="MainLeft" HorizontalAlignment="Left" Height="600" VerticalAlignment="Top" Width="1000">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" MinWidth="200" MaxWidth="495" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" MinWidth="500" />
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalScrollBarVisibility="Visible">
            <TextBox x:Name="textBox" Text="Left" />
        </ScrollViewer>
        <GridSplitter Grid.Column="1" x:Name="gridSplitter" HorizontalAlignment="Center" Height="auto" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="5" ResizeDirection="Columns" Background="#FF464444"/>
        <ScrollViewer Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalScrollBarVisibility="Visible">
            <TextBox x:Name="textbox1" Text="Right" />
        </ScrollViewer>
</Grid>

试试下面的

<Grid x:Name="GridLeft" HorizontalAlignment="Left" Height="600" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="200"/>
            <ColumnDefinition Width="5" />
            <ColumnDefinition MinWidth="500"/>
        </Grid.ColumnDefinitions>
</Grid>

Width="*"将用Width="*"的计数除以总空间,并设置列的宽度。因此删除它,如果需要,可以使用Width="Auto",它将占用列内项目的宽度。

相关内容

  • 没有找到相关文章

最新更新