如何使工具栏中的文本框可伸缩

时间:2014-01-20 08:27:41

标签: wpf xaml

我有一个wpf浏览器,Textbox附加Toolbar。但是,我觉得不可能让它变得可伸缩。我正在寻找的效果就是你在Chrome中看到的那种效果。尝试调整浏览器窗口的大小并观察搜索字段,您将明白我的意思。

Toolbar已经非常局限于所有编辑,但我不能忽视它应该是这个问题的解决方案。

到目前为止,我唯一能做的就是根据其中的字符数量来制作Textbox比例,但这显然不是我想要的。

另外:我的Toolbar伸展得很好。

我的XAML代码如下:

<ToolBar HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="49" Width="auto">
    <TextBox x:Name="Urlbox" Height="44" Margin="0" TextWrapping="Wrap" Text="Input URL" GotFocus="TextBox_GotFocus" KeyDown="OnKeyDownHandler" VerticalAlignment="Top" Width="260" HorizontalContentAlignment="stretch"/>
</ToolBar>

3 个答案:

答案 0 :(得分:0)

TextBlock放入StackPanelGrid,这样就可以了。

<ToolBar HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="49" Width="auto">    
    <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToolBar}}, Path=ActualWidth}">
       <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" ... >
    </Grid>
</ToolBar>

答案 1 :(得分:0)

从TextBox中删除Width =“260”属性。并添加Horizo​​ntalAligment =“Stretch”。

答案 2 :(得分:0)

您现有的代码有很多东西意味着TextBox不会调整大小。

  1. 你有一个固定的宽度
  2. 您已在TextBox
  3. 上设置HorizontalContentAlignment="Stretch"但未设置HorizontalAlignment="Stretch"
  4. 您已在工具栏上设置HorizontalAlignment="Stretch"但未设置HorizontalContentAlignment="Stretch"

  5. <ToolBar HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" Height="49" Width="auto">
        <TextBox x:Name="Urlbox" Height="44" Margin="0" TextWrapping="Wrap" Text="Input URL" GotFocus="TextBox_GotFocus" KeyDown="OnKeyDownHandler" VerticalAlignment="Top"  HorizontalContentAlignment="stretch" HorizontalAlignment="stretch"/>
    </ToolBar>
    

    如果没有其他人说过,你可以尝试在GridBar中放置一个Grid(不是StackPanel),并在该Grid中放置TextBox。网格将自动占用其父级给出的空间(相当于Width =“*”),而StackPanel只占用内容所需的空间(相当于Width =“Auto”)。