Grid中的列表框不会调整大小

时间:2016-05-16 18:01:50

标签: c# xaml listbox

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Rectangle Fill="#8686EA" Grid.Column="0" />
    <Rectangle Fill="#FFFFE1" Grid.Column="1" />
    <TextBlock x:Name="NickNameBlock" HorizontalAlignment="Left" Margin="10,22,0,0" TextWrapping="Wrap" Text="Nickname" VerticalAlignment="Top"/>
    <TextBox x:Name="NickName" HorizontalAlignment="Left" Margin="84,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="109"/>
    <TextBlock x:Name="ChannelBlock" HorizontalAlignment="Left" Margin="10,59,0,0" TextWrapping="Wrap" Text="Channel" VerticalAlignment="Top"/>
    <TextBox x:Name="Channel" HorizontalAlignment="Left" Margin="84,47,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="109"/>
    <Button x:Name="JoinButton" Content="Join" HorizontalAlignment="Left" Margin="198,47,0,0" VerticalAlignment="Top"/>
    <Grid Grid.Column="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="630" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Rectangle Fill="white" Stroke="Black" StrokeThickness="0" Grid.Column="0" Margin="10,27,0,100" />
        <!--<Rectangle Fill="white" Stroke="Black" StrokeThickness="1" Grid.Column="1" Margin="6,10,71,196" />-->
        <Grid Grid.Column="1" Margin="0,0,0,196">
            <Grid.RowDefinitions>
                <RowDefinition Height="40"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Rectangle Fill="#8686EA" Stroke="Black" StrokeThickness="0" Grid.Row="0" Margin="10,0,0,0" />
            <Rectangle Fill="#CCCCFF" Stroke="Black" StrokeThickness="0" Grid.Row="1" Margin="10,0,0,-5" RenderTransformOrigin="0.505,0.719" Grid.RowSpan="2" />
            <ListBox x:Name="listBox" Grid.Row="2" Margin="10,10,0,0"  VerticalAlignment="Top" VerticalContentAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Visible">
                <ListBox.Items>
                    <ListBoxItem Content="Tewl"/>
                    <ListBoxItem Content="Ryu"/>
                    <ListBoxItem Content="Clint"/>
                    <ListBoxItem Content="Bak3r"/>
                    <ListBoxItem Content="sm0kex"/>
                    <ListBoxItem Content="Jack"/>
                    <ListBoxItem Content="Bam"/>
                    <ListBoxItem Content="Jay"/>
                    <ListBoxItem Content="Tewl"/>
                    <ListBoxItem Content="Ryu"/>
                    <ListBoxItem Content="Clint"/>
                    <ListBoxItem Content="Bak3r"/>
                    <ListBoxItem Content="sm0kex"/>
                    <ListBoxItem Content="Jack"/>
                    <ListBoxItem Content="Bam"/>
                    <ListBoxItem Content="Jay"/>
                    <ListBoxItem Content="Tewl"/>
                    <ListBoxItem Content="Ryu"/>
                    <ListBoxItem Content="Clint"/>
                    <ListBoxItem Content="Bak3r"/>
                    <ListBoxItem Content="sm0kex"/>
                    <ListBoxItem Content="Jack"/>
                    <ListBoxItem Content="Bam"/>
                    <ListBoxItem Content="Jay"/>
                </ListBox.Items>
            </ListBox>
        </Grid>
    </Grid>
</Grid>

updated listbox in full mode updated nicklist resized still wont expand listbox

这是为Chris W.重新更新的。这有整个代码..我的问题必须更高我真的讨厌宽度/高度/边距..

2 个答案:

答案 0 :(得分:0)

将最后一个RowDefinition设置为*,删除边距并更改VerticalAlignment = Top似乎对我有效。

    <Grid Grid.Column="1" Margin="0,0,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Rectangle Fill="#8686EA" Stroke="Black" StrokeThickness="0" Grid.Row="0" Margin="10,0,0,0" />
        <Rectangle Fill="#CCCCFF" Stroke="Black" StrokeThickness="0" Grid.Row="1" Margin="10,0,0,0" RenderTransformOrigin="0.505,0.719" />
        <ListBox x:Name="listBox" Grid.Row="2" Margin="10,0,0, 0" VerticalAlignment="Top">
            <ListBox.Items>
                <ListBoxItem Content="Tewl"/>
                <ListBoxItem Content="Ryu"/>
                <ListBoxItem Content="Clint"/>
                <ListBoxItem Content="Bak3r"/>
                <ListBoxItem Content="sm0kex"/>
                <ListBoxItem Content="Jack"/>
                <ListBoxItem Content="Bam"/>
                <ListBoxItem Content="Jay"/>
            </ListBox.Items>
        </ListBox>
    </Grid>

答案 1 :(得分:0)

所以你的边距和行p以及不必要的混乱只会让你在未来更加头疼。如果你不介意我只是继续前进并重新写下你在那里所做的一切,并做到这一切,这样它就会响应任何调整大小和你做的事情。请参阅下面的更改,其中包括您的答案。如果是我,我会做更像这样的事情;

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition MinWidth="200" Width="Auto"/>
    </Grid.ColumnDefinitions>

    <!-- Left Pane -->
    <Grid VerticalAlignment="Top" Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.Resources>
            <Style TargetType="TextBox">
                <Setter Property="MinHeight" Value="26"/>
                <Setter Property="Margin" Value="5"/>
            </Style>
            <Style TargetType="TextBlock">
                <Setter Property="FontWeight" Value="SemiBold"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="HorizontalAlignment" Value="Right"/>
            </Style>
        </Grid.Resources>

        <!-- These didn't seem to have a purpose, so commenting out for now.
        <Rectangle Fill="#8686EA" Grid.Column="0" />
        <Rectangle Fill="#FFFFE1" Grid.Column="1" />
        -->

        <TextBlock x:Name="NickNameBlock" Text="Nickname"/>
        <TextBox Grid.Column="1" x:Name="NickName" 
                 TextWrapping="Wrap" Text=""/>

        <TextBlock Grid.Row="1" x:Name="ChannelBlock" Text="Channel" />
        <TextBox Grid.Row="1" Grid.Column="1" x:Name="Channel" 
                 TextWrapping="Wrap" Text=""/>

        <Button Grid.Row="1" Grid.Column="2" x:Name="JoinButton" Content="Join"
                MinWidth="50" Margin="0,5"/>

    </Grid>
    <!-- End Left Pane -->

    <!-- Center Pane -->
    <Rectangle Grid.Column="1" Fill="white"/>
    <!-- End Center Pane -->

    <!-- Right Pane -->
    <Grid Grid.Column="2" Background="#CCCCFF">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Rectangle Fill="#8686EA"/>

        <ListBox x:Name="listBox" Grid.Row="2" 
                 ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ListBox.Items>
                <ListBoxItem Content="Tewl"/>
                <ListBoxItem Content="Ryu"/>
                <ListBoxItem Content="Clint"/>
                <ListBoxItem Content="Bak3r"/>
                <ListBoxItem Content="sm0kex"/>
                <ListBoxItem Content="Jack"/>
                <ListBoxItem Content="Bam"/>
                <ListBoxItem Content="Jay"/>
                <ListBoxItem Content="Tewl"/>
                <ListBoxItem Content="Ryu"/>
                <ListBoxItem Content="Clint"/>
                <ListBoxItem Content="Bak3r"/>
                <ListBoxItem Content="sm0kex"/>
                <ListBoxItem Content="Jack"/>
                <ListBoxItem Content="Bam"/>
                <ListBoxItem Content="Jay"/>
                <ListBoxItem Content="Tewl"/>
                <ListBoxItem Content="Ryu"/>
                <ListBoxItem Content="Clint"/>
                <ListBoxItem Content="Bak3r"/>
                <ListBoxItem Content="sm0kex"/>
                <ListBoxItem Content="Jack"/>
                <ListBoxItem Content="Bam"/>
                <ListBoxItem Content="Jay"/>
            </ListBox.Items>
        </ListBox>
    </Grid>
    <!-- End Right Pane -->

</Grid>

结果:

enter image description here

希望这会有所帮助,欢呼。