WPF ColumnDefinitions和起始宽度

时间:2015-11-30 16:47:10

标签: wpf xaml resizable column-width columndefinition

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

<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>

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

当前最新代码:

2012-10-09  Shop1   0   Trial
2012-10-23  Shop1   0   New
2012-10-30  Shop1   0   New
2012-11-13  Shop1   0   New
2012-11-27  Shop1   1   New
2012-12-11  Shop1   0   New
2012-12-18  Shop1   0   New

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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",它将获取列中项目的宽度。