1

我在设置网格中的列宽时遇到了一些麻烦。我想实现最左边的列在启动时(应用程序)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>
4

2 回答 2

1

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

我在左侧框中设置了 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>
于 2015-11-30T17:42:40.400 回答
0

尝试如下,

<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"它来获取列内项目的宽度。

于 2015-11-30T16:54:51.813 回答