Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align TextBox inside a TextBlock

Tags:

.net

wpf

I want to display a short sentence with a TextBox control in the middle of wrapping text where the user shall enter a value. I'm not sure whether it's a good idea to do this:

<TextBlock TextWrapping="Wrap">
    <Run Text="Keep at least"/>
    <TextBox Width="30" Margin="4,0"/>
    <Run Text="MB free on the drive"/>
</TextBlock>

But it works and wraps the input field along with the text. Unfortunately, the TextBox is baseline-aligned with the text, not centered. This means that the text within the input box is not on the same height as the label around it. Obvious vertical alignment attributes don't help me.

Is there any solution to this, or a different method altogether?

HTML can do this just fine, how about WPF?

like image 783
ygoe Avatar asked Jun 07 '26 18:06

ygoe


1 Answers

I think this should help.

<TextBlock TextWrapping="Wrap">
    <Run Text="Keep at least"/>
    <InlineUIContainer BaselineAlignment="Center">
        <TextBox Width="30" Margin="4,0"/>
    </InlineUIContainer>
    <Run Text="MB free on the drive"/>
</TextBlock>
like image 187
Szabolcs Dézsi Avatar answered Jun 10 '26 10:06

Szabolcs Dézsi