【问题标题】:Troubles with resizing UserControl children or perhaps a better method than using an UserControl?调整 UserControl 子级大小的问题或者比使用 UserControl 更好的方法?
【发布时间】:2016-08-20 07:07:44
【问题描述】:

我目前正在开发一个 WPF 小程序,作为离线工单的模板和记录器。

我的程序设计目前基于创建一个用户控件作为票证。此票证 (UserControl) 是一组文本字段。这是我的用户控件的屏幕截图。

Ticket (UserControl)

然后我的程序读取票证中的每个字段并将其存储在文本文件中。

这一切都适用于调整大小。 WPF 很难调整大小(在我看来)。我对 WPF 没有太多经验,但我无法让 UserControl 在我在主窗口中使用的网格上调整大小。控件调整大小,但我的用户控件的子级都没有调整大小。

这是我的主窗口的外观

Main Window

工单被添加到工单选项卡下的网格中。这里调整大小的所有内容都期望 UserControl 的子项(文本字段、按钮等)。

问题:有没有更好的方法来制作这样的东西(IE 不使用用户控件)或者我只需要使用一个凌乱的网格,以及大量的拉伸和自动属性我的 UserControl 和她的孩子们让这个工作?

也许我只是在这种情况下完全错误地使用了 UserControls。

【问题讨论】:

    标签: c# wpf xaml user-controls


    【解决方案1】:

    大多数 WPF 控件(以及用户控件)的默认行为是扩展并占用尽可能多的可用空间

    例如(为清楚起见省略了 xml 命名空间)

    <Window>
      <Button Content="Hello" />
    </Window>
    

    如果你最大化你的窗口会创建一个大按钮

    在您的情况下,我猜您正在使用 Visual Studio 设计器来对齐和调整各个控件的大小。设计器生成的代码的一种行为是设置Margin 属性,如果您移动控件以定位它们,则设置WidthHeight,如果您调整大小。这会导致它们停止扩展或拉伸。

    通常,我避免使用可视化设计器,而是直接使用 XAML 编辑器来设计布局并验证它在可视化设计器上的显示是否符合预期。

    虽然根据屏幕可用性扩展和收缩控件很不错,但有时可能会导致布局和尺寸不佳或奇怪。通常,当我使用Grid 作为布局时,我会设置各个子级的MinWidthMinHeightMaxWidthMaxHeight,用于在拉伸或压缩过多时看起来不太好的控件(RadioButtons、CheckBox) .对于某些控件,最好仅水平拉伸(单行文本输入在您的情况下为问题定义字段),其中将Grid 行高设置为auto 是明智的,以便控件获得足够的高度它需要。

    如果窗口尺寸太小,也可以使用具有合理最小/最大宽度约束的GridSplitter 来允许用户调整 LHS 列的大小

    类似的东西(虽然没有测试)

    <Window>
      <Window.Resources>
        <Style TargetType="TextBox">
          <Setter Property="Margin" Value="5" />
        </Style>
        <Style TargetType="Button">
          <Setter Property="Margin" Value="5" />
        </Style>
        <Style TargetType="TextBlock">
          <Setter Property="Margin" Value="5" />
          <Setter Property="FontWeight" Value="Bold"/>              
        </Style>
      </Window.Resources>
    
      <Grid>  
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="0.3*" MinWidth="200" MaxWidth="350" />
          <ColumnDefinition Width="0.7*" />
        </Grid.ColumnDefinitions>    
    
        <Grid.RowDefinitions>
          <RowDefinition Height="auto"/>
          <RowDefinition Height="auto"/>
          <RowDefinition Height="auto"/>
    
          <RowDefinition Height="auto"/>
          <RowDefinition Height="auto"/>
          <RowDefinition Height="auto"/>
    
          <RowDefinition Height="auto"/>
          <RowDefinition Height="auto"/>
          <RowDefinition Height="auto"/>
    
          <RowDefinition Height="*"/>
          <RowDefinition Height="auto"/>
          <RowDefinition Height="auto"/>
    
        </Grid.RowDefinitions>
    
        <!-- first column -->
    
        <TextBlock Grid.Column="0"
                   Grid.Row="0"
                   Text="Employee ID" />
    
        <TextBox Grid.Column="0"
                 Grid.Row="1"
                 Text="{Binding EmployeeID}" />
    
        <TextBlock Grid.Column="0"
                   Grid.Row="2"
                   Text="User Name" />
    
        <TextBox Grid.Column="0"
                 Grid.Row="3"
                 Text="{Binding UseName}" />
    
        <TextBlock Grid.Column="0"
                   Grid.Row="4"
                   Text="Computer Name" />
    
        <TextBox Grid.Column="0"
                  Grid.Row="5"
                  Text="{Binding ComputerName}" />
    
        <TextBlock Grid.Column="0"
                   Grid.Row="6"
                   Text="PhoneNumber" />
    
        <TextBox Grid.Column="0"
                  Grid.Row="7"
                  Text="{Binding PhoneNumber}" />
    
        <TextBlock Grid.Column="0"
                   Grid.Row="8"
                   Text="Location" />
    
        <TextBox Grid.Column="0"
                 Grid.Row="9"
                 Grid.RowSpan="2"
                 Text="{Binding Location}" />
    
        <Button Grid.Column="0"
                Grid.Row="11"
                Text="Copy All" />
    
        <!-- second column -->
    
        <TextBlock Grid.Column="1"
                   Grid.Row="0"
                   Text="Problem Description" />
    
        <Grid Grid.Column="1"
              Grid.Row="1">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
          </Grid.ColumnDefinitions>    
    
          <TextBox Grid.Column="0"
                   Text="{Binding ProblemDescription}" />    
    
          <Button Grid.Column="1"
                  Content="C" />
        </Grid>
    
        <TextBlock Grid.Column="1"
                   Grid.Row="2"
                   Text="Notes" />
    
        <TextBox Grid.Column="0"
                 Grid.Row="3"
                 Grid.RowSpan="7"
                 Text="{Binding Notes}" />    
    
        <TextBlock Grid.Column="1"
                   Grid.Row="10"
                   Text="Resolution" />
    
        <Grid Grid.Column="1"
              Grid.Row="11">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
          </Grid.ColumnDefinitions>    
    
          <TextBox Grid.Column="0"
                   Text="{Binding Resolution}" />    
    
          <Button Grid.Column="1"
                  Content="C" />
        </Grid>
    
        <!-- splitter to make the columns resizable -->
    
        <GridSplitter Grid.Column="1"
                      Grid.Row="0"
                      Grid.RowSpan="12"
                      Width="3"
                      Background="Blue" />    
      </Grid>
    </Window>
    

    【讨论】:

    • 是的,我会试一试。我试图避免使用带有列和行的网格。但是,在阅读了您提出的内容并进行了一些研究之后,似乎这可能是做我想做的事情的唯一方法。
    猜你喜欢
    • 2012-05-26
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多