C# 如何让控件填充所有可用空间

C# 如何让控件填充所有可用空间,c#,wpf,xaml,C#,Wpf,Xaml,我有一个xaml代码: <Grid> <WrapPanel> <TextBox ></TextBox> <Button Content="GetIt" /> </WrapPanel> </Grid> 如何为textBox获取所有可用空间 我想这样做: |[\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

我有一个xaml代码:

<Grid>
    <WrapPanel>
    <TextBox ></TextBox>
    <Button Content="GetIt" />
    </WrapPanel>
</Grid>

如何为textBox获取所有可用空间

我想这样做:

|[\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu|


有很多方法可以实现这一点,包括:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <TextBox />
  <Button Grid.Column="1">GetIt</Button>
</Grid>

格蒂
试试这个:

<Grid>
    <TextBox HorizontalAlignment="Stretch" Margin="2,2,102,2"></TextBox>
    <Button HorizontalAlignment="Right" Width="100" Content="GetIt" />
</Grid>

只需将按钮设置为所需的宽度,文本框就会填满剩余的部分



谢谢你的接球;上面已更正,以正确处理右侧的边距。但是,当按钮宽度更改时,这确实需要您更新边距。如果计划经常更改间距,则两列是更好的解决方案。如果网格中有多个控件,并且不想创建嵌套网格来处理此类拆分,则使用边距会更干净。

最简单的方法是使用DockPanel而不是网格(LastChildFill的默认值为true,但为了清晰起见,我在此处添加了它):


以下是一种实现所需布局的方法:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <Style TargetType="TextBox">
      <Setter Property="Margin" Value="2"/>
    </Style>
  </Page.Resources>
  <DockPanel>
    <DockPanel DockPanel.Dock="Top">
      <!-- Because the Button is fixed in size, you can divide the row it's 
      in using a DockPanel:  the Button is docked to the right edge, and the
      TextBox fills up the remaining available space. -->
      <Button Margin="2" Padding="2" DockPanel.Dock="Right">GetIt</Button>
      <TextBox />
    </DockPanel>
    <!-- Because the TextBoxes *aren't* fixed in size, you can't use docking,
    as it won't size them.  So put them in a Grid and use star sizing to
    divide the grid's vertical space into two equal parts.   The Grid will
    fill up the remainder of the (outer) DockPanel. -->
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <TextBox Grid.Row="0">Another TextBox</TextBox>
      <TextBox Grid.Row="1">Yet another TextBox</TextBox>
    </Grid>
  </DockPanel>
</Page>

格蒂
另一个文本框
又是一个文本框

WrapPanel的用途是什么?我只是不知道有什么更好的方法可以做到这一点:但在这种情况下,按钮位于文本框上方。在这里你可以看到截图,我打赌这是一种技术,它来自于学习如何在没有表格的CSS中进行布局。如果你在网格中删除和调整控件大小,它也是一种代码,Blend会“猜测”。谢谢!我能再问一个问题吗?如何粘合网格列|这里是另一个多行文本框。哦,对不起。我的评论格式已丢失。我的意思是,你不想得到DockPanel设计的确切效果。使用网格也可以做同样的事情,它更灵活,但是如果需要添加或删除项,它需要更多的XAML,并且需要大量维护行/列/行/列值。
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <Style TargetType="TextBox">
      <Setter Property="Margin" Value="2"/>
    </Style>
  </Page.Resources>
  <DockPanel>
    <DockPanel DockPanel.Dock="Top">
      <!-- Because the Button is fixed in size, you can divide the row it's 
      in using a DockPanel:  the Button is docked to the right edge, and the
      TextBox fills up the remaining available space. -->
      <Button Margin="2" Padding="2" DockPanel.Dock="Right">GetIt</Button>
      <TextBox />
    </DockPanel>
    <!-- Because the TextBoxes *aren't* fixed in size, you can't use docking,
    as it won't size them.  So put them in a Grid and use star sizing to
    divide the grid's vertical space into two equal parts.   The Grid will
    fill up the remainder of the (outer) DockPanel. -->
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <TextBox Grid.Row="0">Another TextBox</TextBox>
      <TextBox Grid.Row="1">Yet another TextBox</TextBox>
    </Grid>
  </DockPanel>
</Page>