WPF中的码头和锚点

马特

我来自WinForms,并且开始学习WPF。我已经在Pluralsight上完成了一系列教程,但是在处理调整大小时遇到​​了一些困难。

调整大小时,我的文本框似乎并没有像我想要的那样“固定”。我将下面的xaml包括在其中,以说明我要寻找的行为。任何有关最佳做法的反馈将不胜感激。该代码对我来说有点“尴尬”,但是我不确定这是否是因为它对我来说只是新事物,或者是否有更简便/更好的方法来进行我的尝试。

为了了解外观,而无需在下面加载XAML,这里是调整表单屏幕截图大小之前和之后的内容。调整大小之前http://dboasis.com/screenshots/beforeresize.jpg 调整大小之前http://dboasis.com/screenshots/afterresize.jpg

在查看了我如何在XAML中尝试进行调整大小问题以及最佳实践之后,我希望能获得一些建议。

另外-xaml确实实现了DevExpress控件-如果有人希望我重做表单而不使用第三者控件,以便他们更轻松地提出建议,我很乐意这样做。

<dx:DXWindow
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="DXTemplateSandbox.MainWindow"
    Title="MainWindow" Height="437" Width="641">
<Grid>
    <dx:DXTabControl x:Name="tabcntMainTab">
        <dx:DXTabItem x:Name="tabUserList" Header="User List">
            <Grid Background="Transparent">

                <Grid.RowDefinitions>
                    <RowDefinition x:Name="SelectLabel" Height="30" />
                    <RowDefinition x:Name="OpenDataFile" Height="34" />
                    <RowDefinition x:Name="DataGridLayoutRow" Height="185*" />
                    <RowDefinition x:Name="AppPrefsInfo" Height="110" />
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10,0">
                    <!-- 
                    The DataFileLocation not resizing the width.  How do I instead lock the vertical size, but allow width to
                    resize with form?
                    -->
                    <TextBox Name="DataFileLocation" Width="419" Margin="0,5" HorizontalAlignment="Stretch" />

                    <!--
                    How do I get the SelectData button stay immediately to the right of the DatFileLocation textbox when resizing?
                    -->
                    <Button Name="SelectData" Content="..." Width="40" Margin="5" Click="SelectData_Click"/>
                    <DockPanel>
                        <!-- I would like the "Go" button to stay anchored to the right when resizing. -->
                        <Button x:Name="GoButton" 
                                Content="Go" 
                                Width="66" 
                                DockPanel.Dock="Right"
                                HorizontalAlignment="Right"
                                VerticalAlignment="Top"
                                Margin="50,5,5,5" 
                                Click="GoButton_Click" />
                    </DockPanel>

                </StackPanel>

                <!--
                -->
                <dxg:GridControl Grid.Row="2" Margin="5" >
                    <dxg:GridControl.View>
                        <dxg:TableView ShowGroupPanel="False"/>
                    </dxg:GridControl.View>
                </dxg:GridControl>

                <Label Content="Select Data File" HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Top" Height="26" Grid.RowSpan="2" Width="91"/>

                <!-- 
                Is using a grid inside a parent grid cell the best way of doing this?  I'm using stackpanels in each of the rows in the
                child grid.  Is there a better way of doing this?  
                -->
                <Grid x:Name="AppPrefsGrid" Grid.Row="3" >
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <!--
                    Ideally I would like the text boxes to grow and shrink at the same rate as form is resized, and leave the labels the same width.  I don't know
                    if this is practical however.
                    -->
                    <StackPanel Grid.Row="0" Orientation="Horizontal">
                        <Label Content="Registration Name:" Width="112" Margin="5"/>
                        <TextBox Name="RegNameTextBox" Width="175" Margin="5" IsEnabled="False"/>
                        <Label Content="Install Date:" Width="84" Margin="10,5,5,5"/>
                        <TextBox Name="InstallDateTextBox" Width="175" Margin="5" IsEnabled="False"/>
                    </StackPanel>
                    <StackPanel Grid.Row="1" Orientation="Horizontal">
                        <Label Content="Registration Number:" Width="112" Margin="5"/>
                        <TextBox Name="RegNumberTextBox" Width="175" Margin="5" IsEnabled="False"/>
                        <Label Content="Data File Version:" Width="84" Margin="10,5,5,5"/>
                        <TextBox Name="DataVersionTextBox" Width="175" Margin="5" IsEnabled="False"/>
                    </StackPanel>
                    <StackPanel Grid.Row="2" Orientation="Horizontal">
                        <Label Content="Edition Code:" Width="112" Margin="5"/>
                        <TextBox Name="EditionCodeTextBox" Width="175" Margin="5" IsEnabled="False"/>
                        <Label Content="User Count:" Width="84" Margin="10,5,5,5"/>
                        <TextBox Name="UserCountTextBox" Width="175" Margin="5" IsEnabled="False"/>
                    </StackPanel>


                </Grid>

            </Grid>

        </dx:DXTabItem>
        <dx:DXTabItem x:Name="tabKeyGen" Header="Key Generator"/>
    </dx:DXTabControl>
</Grid>

瑞秋

您这里有多个问题,这似乎是因为您没有为控件使用正确的布局面板。

默认情况下,控件的大小基于父控件的行为。

例如,放置在中的项目Grid将拉伸以填充Grid Cell中为其分配的所有可用空间。如果将“网格行/列”定义的“高度/宽度”设置为“高/宽”,Auto则表示将以控件所需的大小绘制控件。如果将其设置为固定大小(如)100,则它将以该大小绘制控件。如果将其设置为*大小,它将使用剩余空间的百分比绘制控件。

这种行为可以通过性能上的控制本身,如被覆盖HorizontalAlignmentVerticalAlignment,或设置属性,如HeightWidth,和Margin

因此,要解决您的某些特定问题,请先移除托管标签/文本框组合的那些堆叠面板,然后将其替换为适当的3x4 Grid将标签放在第0列和第2列中,并指定ColumnDefinition固定大小。将您的文本框放在第1列和第3列中,并保留它们的*大小。请记住,*大小是一个百分比,因此,一列中的2 *和另一列中的3 *表示存在5*总计,列1将占据2/5的空间,而列2将占据3/5的空间。

要使您DataFileLocation随着表单的大小而调整大小,只需删除Width属性,然后将其大小调整为网格单元格的大小即可。

要使SelectData按钮保持停靠在SelectData文本框的右侧,请将两者都放在中DockPanel将按钮停靠在右侧,并允许TextBox填充所有剩余空间。如果希望Tab键按预期方式在它们之间循环,则必须设置TabIndex。

<DockPanel>
    <Button DockPanel.Dock="Right" ... />
    <TextBox ... />
</DockPanel>

首先,看看它的外观。父网格的RowDefinition看起来正确(除了第3行的高度应该为"*"并且不是"184*"),这意味着您的DevExpress控件应该拉伸/缩小以填充所有剩余的垂直空间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

递归查询列中的锚点和递归部分之间的类型不匹配

来自分类Dev

锚点和输入字段在IE11中不起作用

来自分类Dev

在嵌套的Divs中获取锚点InnerText

来自分类Dev

Sencha ExtJs 5中的锚点布局和配合布局之间的区别

来自分类Dev

友好的网址和锚点

来自分类Dev

从网址中删除锚点

来自分类Dev

引导程序中锚点和输入组的垂直对齐

来自分类Dev

如何在jsPlumb中动态更改targetOption和sourceOption锚点?

来自分类Dev

如何在React中偏移链接和锚点?

来自分类Dev

使用ImportXML提取URL和锚点

来自分类Dev

从URL和历史记录中删除锚点

来自分类Dev

锚点click和href的angularjs

来自分类Dev

PHP:[[:<:]]和[[:>:]]锚点

来自分类Dev

指定的锚点链接在dojo对话框中不起作用(chrome和firefox)

来自分类Dev

在CSS中对齐跨度,段落和锚点

来自分类Dev

在绝对位置的div中垂直和水平对齐锚点

来自分类Dev

WPF:锚点有时效果不佳

来自分类Dev

链接到不同页面上的锚点和scroolTo使用JQuery锚点

来自分类Dev

C#正则表达式中的角色类和锚点

来自分类Dev

在SVG中获取锚点元素

来自分类Dev

链接到框架中的锚点

来自分类Dev

jQuery滚动和锚点初始位置

来自分类Dev

更改特定div中的锚点

来自分类Dev

添加和删除锚点属性

来自分类Dev

从网址中删除锚点

来自分类Dev

Django无尽的分页和锚点

来自分类Dev

使用锚点,类和jQuery滚动

来自分类Dev

锚点和按钮内部的垂直定位

来自分类Dev

在 Markdown 中稳健地生成锚点