如何设置TexTbox根据TextBlock自动调整大小。 WPF

时间:2016-06-14 18:11:15

标签: .net wpf xaml textbox textblock

我是WPF的新手,我需要一点帮助。

我想制作像TextBlock这样的东西,并且在它前面有一个TextBox。 但是,我的应用程序是可本地化的,因此TextBlock有时可能包含长文本,有时短,所以我希望文本框根据TextBlock的宽度自动调整大小。

我尝试使用stackpanel但是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>

每当文本块获得更大的文本时,文本框实际上移动到右边并且没有调整大小..

我只需要被推向正确的方式,任何帮助都会受到赞赏。

E:我目前使用网格的代码:

<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>

2 个答案:

答案 0 :(得分:2)

与评论中建议的Karmacon一样,使用Grid最简单。 (编辑:也许不是最容易的,但它可能是提供你需要的确切结果的那个)

<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>

当然,我根据一些假设做了一些改变,我相信这很明显。

答案 1 :(得分:1)

请改用DockPanel。

<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>