Search code examples
wpftextboxvariable-width

WPF TextBox - how to stop width change with value


I have a problem setting my TextBox exactly as I need. I was reading similar questions here, but none of them have same requirements as me.

Basicaly I need text box with some MaxWidth to always try to fit the MaxWidth property. If user changes size of the window, it should shrink if needed, or grow up to the MaxWidth. But at the same time, I need it to be alignet to the left, to create this look: desired look

But when I set HorizontalAlignment to "Left" - the text box change width based on inner value. When I set HorizontalAlignment to "Stretch" - then the text box is in middle of the screen, not by left side next to the buttons.

I need some combination of these two properties, but I have no idea how I should do it.

I do not use GridColumns as my layout, as there are other problems, thanks to which I am not able to create my desired result.

Here is example of my ButtonEdit - which is TextBox with buttons:

 <dxe:ButtonEdit 
        x:Name="SearchPanel"
        Margin="52,4,0,0"            
        HorizontalAlignment="Left"       
        VerticalAlignment="Top"      
        NullText="Zadejte hledaný text.."
        Height="23" MaxWidth="200" AllowDefaultButton="False"
        Grid.Row="0">
        <dxe:ButtonEdit.Buttons>
            <dxe:ButtonInfo
                ...
            </dxe:ButtonInfo>
            <dxe:ButtonInfo
                ...
            </dxe:ButtonInfo>
        </dxe:ButtonEdit.Buttons>
    </dxe:ButtonEdit>

Can someone help me please?

Thank you.


Solution

  • Ok, I managed to solve this, the trick is to calculate With in runtime using bindings. As I use DevExpress controls, this is the solution:

    Width="{DXBinding '@a($Grid).ActualWidth - 50'}"
    

    Or without DevExpress - this should work too:

    {Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=Grid}, Converter={local:WidthConverter}}