【问题标题】:How to make a textbox stretch to its parent window's width in C#?如何在 C# 中使文本框拉伸到其父窗口的宽度?
【发布时间】:2011-11-14 21:01:59
【问题描述】:

我有一个TextBox,我想将它延伸到其父级的Width

我尝试锚定上、左、右,但仅超过 60%,有什么想法吗?

【问题讨论】:

  • 使其与设计器中的父级宽度相同,然后将其锚定到右侧。
  • 你在用什么?表格? ASP.NET? WPF?还有什么?

标签: c# winforms textbox width stretch


【解决方案1】:

在 Windows 应用商店、Windows Phone 8.0 和 8.1、WPF 和所有基于 XAML 的 UI 平台中,可以使用属性

VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"

【讨论】:

    【解决方案2】:

    正如 Hans Passant 在评论中所说,但也锚定在左侧。

    或者,如果您想以编程方式进行:

    textBox1.Width = textBox1.Parent.Width;
    textBox1.Location = new Point(0,textBox1.Location.Y);
    textBox1.Anchor = AnchorStyles.Left | AnchorStyles.Right;
    

    【讨论】:

      【解决方案3】:

      在 WPF 中,对父控件的属性 ActualWidth 使用 Binding(在本例中为它的命名容器):

      Width="{Binding Container, Path=ActualWidth}"
      

      另一种可能性是这样(如果您没有命名控件而只想绑定到父控件):

      Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}"
      

      【讨论】:

      • 他在谈论 WinForms(他提到试图锚定这是 WinForms 唯一的术语)。
      • 哦,好吧..不太了解winforms,他没有说是winforms还是wpf。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2012-12-22
      • 2014-08-27
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2017-08-09
      相关资源
      最近更新 更多