Ahmad45123
Ahmad45123

Reputation: 472

How to set TexTbox to resize automatically depending on TextBlock. WPF

I want to make something like a TextBlock and has a TextBox infront of it. However, my app is localizable so the TextBlock can sometimes contains long text and sometimes short so I want the textbox to resize itself automatically depending on TextBlock's width.

I tried to do it with a stackpanel but failed xD:

<StackPanel Orientation="Horizontal" Width="auto" Height="auto">
    <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" Text="{Resx Key=TextBlock_ProjectName}"/>
    <TextBox x:Name="ProjectNameTextBox" HorizontalAlignment="Right" Margin="10,10,0,0" MinWidth="770" MaxWidth="770"/>
</StackPanel>

Whenever the textblock gets a bigger text, The textbox actually MOVES to the right and not resized..

I just need to be pushed to the right way, any help is appreciated.

E: My current code using Grids:

<Grid>
    <Menu x:Name="menu" Height="34" VerticalAlignment="Top" HorizontalAlignment="Left" Width="885" Grid.ColumnSpan="2">
        <MenuItem Header="{Resx Key=StartupForm_MenuItem_OpenGlobalSettings}" Height="34"/>
    </Menu>
    <TabControl x:Name="MainTabControl" Height="297" Margin="0,39,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="887" Grid.ColumnSpan="2">
        <TabItem Header="{Resx Key=StartupForm_TabItem_NewProject}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" MinWidth="770" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Margin="10,10,0,0" Text="{Resx Key=StartupForm_TextBlock_ProjectName}"/>
                <TextBox Grid.Column="1" x:Name="ProjectNameTextBox" Margin="10,10,0,0"/>

                <TextBlock Grid.Column="0" HorizontalAlignment="Left" Margin="10,47,0,0" TextWrapping="Wrap" Text="{Resx Key=StartupForm_TextBlock_SAMPVersion}" VerticalAlignment="Top"/>
                <ListBox Grid.Column="1" x:Name="listBox" HorizontalAlignment="Left" Height="111" VerticalAlignment="Top" Width="765" Margin="105,47,0,0" BorderBrush="{DynamicResource ControlBorderBrush}" BorderThickness="1" />

                <TextBlock Grid.Column="0" x:Name="textBlock2" HorizontalAlignment="Left" Margin="10,191,0,0" TextWrapping="Wrap" Text="Project Location: " VerticalAlignment="Top"/>
                <CheckBox Grid.Column="1" x:Name="checkBox" Content="Create from pre-existing files." Margin="105,162,-95,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
                <controls1:PathTextBox Grid.Column="1" x:Name="textBox1" Height="28" Margin="105,0,11,39" VerticalAlignment="Bottom" Width="765" RenderTransformOrigin="0.075,-0.154" IsDirectory="True" TheDesc="Please choose a directory for the project."/>
                
                <Button x:Name="button1" Content="Create Project" HorizontalAlignment="Left" Margin="10,219,0,0" VerticalAlignment="Top" Width="861" Style="{DynamicResource AccentedSquareButtonStyle}"/>
            </Grid>
        </TabItem>
        <TabItem Header="Load Project"/>
        <TabItem Header="Recent"/>
    </TabControl>
</Grid>

Upvotes: 1

Views: 2949

Answers (2)

Jai
Jai

Reputation: 8363

Like what Karmacon suggested in the comments, it's easiest to do it with Grid. (Edit: perhaps not easiest, but it's probably the one that gives the exact result you need)

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="*" MinWidth="770" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Margin="10,10,0,0" Text="{Resx Key=TextBlock_ProjectName}"/>
    <TextBox Grid.Column="1" x:Name="ProjectNameTextBox" Margin="10,10,0,0"/>
</Grid>

Of course, I did some changes based on some assumptions, which I believe is pretty obvious.

Upvotes: 2

user2880486
user2880486

Reputation: 1108

Use a DockPanel instead.

<DockPanel>
    <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" Text="{Resx Key=TextBlock_ProjectName}"/>
    <TextBox x:Name="ProjectNameTextBox" HorizontalAlignment="Right" Margin="10,10,0,0" MinWidth="770" MaxWidth="770"/>
</DockPanel>

Upvotes: 1

Related Questions