C# 使WPF文本框拉伸到可用空间,而不是随文本增长

C# 使WPF文本框拉伸到可用空间,而不是随文本增长,c#,.net,wpf,C#,.net,Wpf,如何使文本框拉伸到带有三个点的按钮,但在有人输入大量文本时不覆盖它 我的主窗口.xaml <Window x:Class="Foo.Bar.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:p="clr-namespa

如何使
文本框
拉伸到带有三个点的按钮,但在有人输入大量文本时不覆盖它

我的主窗口.xaml

<Window x:Class="Foo.Bar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:p="clr-namespace:Foo.Bar.Properties"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <StackPanel>
      <DockPanel>
        <Label Name="lblFileName" Content="File"></Label>
        <TextBox Name="txbFileName"></TextBox>
        <Button Name="btnOpenFileDialog" Content="..." HorizontalAlignment="Right"></Button>
      </DockPanel>
      <UniformGrid>
        <Button Name="btnFoo" Content="Foo"></Button>
        <Button Name="btnBar" Content="Bar"></Button>
      </UniformGrid>
    </StackPanel>
  </Grid>
</Window>

它看起来像什么


您可以将文本框的MaxWidth设置为要留在其中的空间宽度。这将需要一点加减法,也许还需要一点估算,但它会起作用。让它实际拉伸似乎有点奇怪,但如果您确实特别想要拉伸,那么示例设置可能是:

<TextBox Name="txbFileName" MaxWidth=300*></TextBox>

在DockPanel上拉伸文本框集
LastChildFill=“true”
,并在最后添加文本框

顺便说一句,如果您正在使用DockPanel,您可以使用
DockPanel.Dock=“Right”
而不是
HorizontalAlignment=“Right”



有趣的方法!正是我想要的。谢谢。这种方法的一个很好的优点是,如果你拉伸主窗口(假设你想这样做),文本框将自动适应新的大小。
<Grid>
    <StackPanel>
        <DockPanel LastChildFill="True">
            <Label Name="lblFileName" Content="File"></Label>
            <Button Name="btnOpenFileDialog" Content="..." DockPanel.Dock="Right"></Button>
            <TextBox Name="txbFileName"></TextBox>
        </DockPanel>
        <UniformGrid>
            <Button Name="btnFoo" Content="Foo"></Button>
            <Button Name="btnBar" Content="Bar"></Button>
        </UniformGrid>
    </StackPanel>
</Grid>