调整窗口大小 [英] Sizing controls to a window

查看:108
本文介绍了调整窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我为您准备了另一个预告片.我在包装面板中有一个列表框.列表框显示按钮,如果按钮太多,则会出现滚动条.现在,按钮连续排列了两个.我需要做的是根据列表框中打开的空间调整两个按钮的大小.这样,当出现滚动条时,它们将用尽所有空白,而当没有滚动条时,它们也将使用所有空白.有人知道吗?
谢谢.

Hi guys I got another brain teaser for you. I have a listbox inside a wrap pannel. The listbox displays buttons and if there is too much buttons a scrollbar appears. Now the buttons are arranged two in a row. What i need to do is size the two buttons according to the space open in the listbox. So that when the scrollbar appears, they use up all the blank space and when no scrollbar is present they also use all the white space. Anyone got an idea?
Thanks.

推荐答案

这很简单,但是实现取决于设计的更多细节.您可以使用DockPanel代替或除此之外使用WrapPanel.通常,您几乎应该永远不要使用显式大小,也永远不要使用手动控制位置.停靠允许将控件的大小保持为跟随容器的大小,并且Window.SizeToContent -将Window的大小保持为其内容的大小(在一个方向或两个方向上).



停靠DockPanel是基于子级中使用的依赖项属性DockPanel.Dock.像这样的东西:

This is pretty easy, but implementation would depend on some more details of the design. You can use DockPanel instead or in addition to WrapPanel. In general, you should almost never use explicit sized and never use manual positions of control. Docking allows to keep size of controls to following the size of container, and Window.SizeToContent — size of Window to the size of its content, in one or in both directions.



Docking in a DockPanel is based on dependency property DockPanel.Dock used in children. Something like that:

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300">
    <DockPanel LastChildFill="True">
        <Border Padding="8" Background="AliceBlue">
            <StackPanel DockPanel.Dock="Left">
                <Button Margin="0 0 0 4">First</Button>
                <Button Margin="0 0 0 4">Second</Button>
                <Button>Third</Button>
            </StackPanel>
        </Border>
        <Label DockPanel.Dock="Top" Margin="8 8 8 0"

            Target="{Binding ElementName=textBox}">_Edit Text:</Label>
        <TextBox DockPanel.Dock="Top" Margin="4 0 8 0" Name="textBox"></TextBox>
        <Label DockPanel.Dock="Top" Margin="8 8 8 0"

            Target="{Binding ElementName=treeView}">_Use Tree View:</Label>
        <TreeView Name="treeView" Background="AntiqueWhite"

            Margin="4 0 8 8"></TreeView>
    </DockPanel>
</Window>



注意:最后一个停靠的元素TreeView没有DockPanel.Dock;由于面板LastChildFill="True"的属性,它是最后一个填充剩余空间的元素.

—SA



Pay attention: last docked element, the TreeView, has no DockPanel.Dock; it is a last element which fills the remaining space due to the property of the panel LastChildFill="True".

—SA


您总是可以将按钮VerticalContentAlignment 设置为stretch.那应该会自动调整按钮的大小.
Well you can always set the button VerticalContentAlignment to stretch. That should resize the button automatically.


这篇关于调整窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆