【问题标题】:Listbox in Grid will not resize网格中的列表框不会调整大小
【发布时间】:2016-05-16 18:01:50
【问题描述】:
<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 重新更新的。这有整个代码..我的问题必须更高我真的讨厌宽度/高度/边距..

【问题讨论】:

  • 删除硬性设置的高度,以及初学者列表框上的负边距.....然后在最后一个 RowDefinition 上将“auto”更改为“*”。
  • @Chris W. 感谢您的支持...自上次发布后修改后,我进行了您告诉我的更改...当窗口未最大化时,我似乎仍然无法获得完整的列表框屏幕[代码]
  • 设置 VerticalContentAlignment=Stretch / VerticalAlignment=Stretch,如果不这样做,那么您的布局结构中存在更高的冲突。
  • 一定是我的上层布局
  • @ChrisW.如果你可以再看一次,我添加了整个 xaml 代码..请

标签: c# xaml listbox


【解决方案1】:

因此,您的边距和行跨度以及不必要的混乱只会让您在未来更加头疼。如果您不介意我只是继续并重新编写了您在那里的内容并完成了所有内容,以便它可以响应您所做的任何调整大小和操作。请参阅下面所做的更改,其中包括您的答案。如果是我,我会做更多这样的事情;

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

结果:

希望这会有所帮助,干杯。

【讨论】:

  • PS 如果你实际上是从头到尾制作一个 IRC 应用程序(强调完成它的部分),我们可以离线交谈,我可能愿意为一个很酷的 UI 贡献一些时间你以它为例来扩展我的投资组合。除了我最终为那些没有完成任务的人做了太多的免费工作,这让我没有任何意义。祝你好运! :)
  • 非常感谢@ChrisW。不,我们非常致力于完成这个...这是一个即将推出的聊天服务器 www.irc7.com,其引擎与原始 msn 聊天几乎相同...我们现在只需要创建一个客户端并摆脱msnchat ocx 没有人愿意再使用了......我很想你能为 gui 设计提供任何帮助
【解决方案2】:

将最后一个 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>

【讨论】:

  • 一定是我的上层阶级
猜你喜欢
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 2015-08-06
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
相关资源
最近更新 更多