【问题标题】:Anchoring WPF controls to stick the position on form resize锚定 WPF 控件以将位置固定在表单调整大小上
【发布时间】:2012-09-05 20:19:59
【问题描述】:

所有 Windows 控件都有一个名为 anchor 的属性,通过它我可以强制控件粘贴在其表面上,如果有人重新调整窗体的大小,控件的高度和宽度也会随着窗体大小增加和减小。

我问了一些人如何在 wpf 中做到这一点,他说如下 WPF 中的“停靠”可以通过设置子控件的 Horizo​​ntalAllignment 和 VerticalAllignment 属性来完成。

WPF 中的“锚定”可以通过在子节点上设置 Margin 和/或在容器上设置 Padding 来完成。

不幸的是,我无法这样做。所以这是我的示例 xaml。

<Window x:Class="WpfApplication1.Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window3" Height="300" Width="515">
<Grid>
    <Label Name="lblName" Content="Enter Name" Height="28" HorizontalAlignment="Left" Margin="-17,5,0,0"  VerticalAlignment="Top" />
    <Label Content="Enter Address" Height="28" HorizontalAlignment="Left" Margin="-17,39,0,0" Name="label2" VerticalAlignment="Top" />
    <Label Content="Enter Company" Height="28" HorizontalAlignment="Left" Margin="-17,68,0,0" Name="label3" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="83,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="222" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="83,73,0,0" Name="textBox2" VerticalAlignment="Top" Width="222" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="83,42,0,0" Name="textBox3" VerticalAlignment="Top" Width="222" />
</Grid>
</Window>

所以请编辑 ny xaml 以实现我正在寻找的内容....谢谢

【问题讨论】:

    标签: wpf


    【解决方案1】:

    我不会为你编写代码,但我会告诉你需要做什么来学习如何正确编码。

    首先,你应该学习what layout controls WPF has。我实际上建议通读 this code project article,其中包括每个布局控件及其工作方式的快速可视化表示。

    您当前的 XAML 错误地使用了 Grid 控件以及当前控件的设置方式,您不妨使用用于静态布局的 Canvas

    如果您通过定义RowDefinitionsColumnDefinitions 并将您的控件分配给特定的Grid.RowGrid.Column 来正确使用Grid 控件,那么您将得到您想要的。 :)

    【讨论】:

    • 老爸的回答,他同时设置了 RowDefinitions 和 ColumnDefinitions 但它仍然没有按照我寻找的方式工作。
    • @Thomas 删除Width/Height 属性,以便允许根据网格单元格的大小进行拉伸。如果您希望控件之间有一些空间,您可以保留 Margin 属性,或者使用额外的 Row 或 Column 定义在控件之间留出空间。
    【解决方案2】:

    这是另一种方法。它会给你静态布局:

    <Grid>
         <Grid.RowDefinitions>
     <RowDefinition Height="auto"/>
      <RowDefinition Height="auto"/>
      <RowDefinition Height="auto"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
      <ColumnDefinition Width="auto"/>
      <ColumnDefinition Width="auto"/>
      </Grid.ColumnDefinitions>
    
      <Label Grid.Column="0" Grid.Row="0" Name="lblName" Content="Enter Name"/>     
      <Label Grid.Column="0" Grid.Row="1" Content="Enter Address" />     
      <Label Grid.Column="0" Grid.Row="2" Content="Enter Company" />     
      <TextBox Grid.Column="1" Grid.Row="0" Width="200"/>     
      <TextBox Grid.Column="1" Grid.Row="1" Width="200" />     
      <TextBox Grid.Column="1" Grid.Row="2" Width="200"/> 
    </Grid>
    

    【讨论】:

    • 它是什么?我问了不同的事情。当窗口重新调整大小时,控件也会按比例调整大小。
    • @Thomas 这应该将控件“锚定”到左上角,并且当窗口大小发生变化时它们不应调整大小。如果您没有得到这种行为或者我误解了这个问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多