3

I am coming from a WinForms background and am starting to learn WPF. I've gone through a set of tutorials at Pluralsight, but am having a little difficulty with handling resizing.

When resizing, my textboxes don't seem to be "anchoring" in the way I would want. I'm including the xaml below with notes in it as to the behavior I am looking for. Any feedback regarding best practices would be very much appreciated. The code just "feels" a little awkward to me, but I'm not sure if that is because it's just new to me, or if there are easier/better ways of doing what I am attempting.

To get an idea of what things look like without needing to load the XAML below - here are before and after resizing the form screenshots. Before Resize http://dboasis.com/screenshots/beforeresize.jpg After Resize http://dboasis.com/screenshots/afterresize.jpg

I'm hoping for suggestions both how to handle the resize issues, as well as best practices after seeing how I've attempted to do it in the XAML.

Also - the xaml does implement DevExpress controls - if anyone wants me to redo the form and not use 3rd party controls so they more easily make suggestions, I am happy to do so.

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

4

1 回答 1

5

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

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

例如,放置在 a 中的项目Grid将拉伸以填充网格单元内给它们的所有可用空间。如果网格行/列定义的高度/宽度设置为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. 将 Button 停靠在右侧,并允许 TextBox 填充所有剩余空间。如果您希望 tab 键按预期循环浏览它们,则必须设置 TabIndex。

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

从它开始,看看它的外观。你的父网格的 RowDefinition 看起来是正确的(除了你的第三行高度应该是"*"而不是"184*"),这应该意味着你的 DevExpress 控件应该拉伸/收缩以填充所有剩余的垂直空间。

于 2013-10-03T20:38:44.723 回答