如何通过用户交互在运行时调整XAML TextBox的大小?

时间:2015-09-20 23:46:41

标签: c# xaml textbox windows-runtime winrt-xaml

我想知道是否可以在运行时和用户交互期间调整XAML TextBox的大小。也就是说,用户根据需要使用把手手动调整TextBox的大小。虽然TextBox似乎有一个sizeChanged事件,但我无法弄清楚如何在运行时手动更改大小。

2 个答案:

答案 0 :(得分:2)

这是创建效果的XAML:

<Grid x:Name="MyTextBox" Width="250"
        MinWidth="250" MinHeight="60"
        Margin="20" HorizontalAlignment="Left"
        VerticalAlignment="Top">
    <Grid.Resources>
        <Style TargetType="Polygon">
            <Setter Property="Fill" Value="{ThemeResource SystemControlBackgroundAltHighBrush}" />
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="IsHitTestVisible" Value="False" />
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <CompositeTransform TranslateX="5" TranslateY="5" />
                </Setter.Value>
            </Setter>
            <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
            <Setter Property="Stroke" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}" />
            <Setter Property="StrokeThickness" Value="{ThemeResource TextControlBorderThemeThickness}" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="AcceptsReturn" Value="True" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="TextWrapping" Value="Wrap" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
        </Style>
        <Style TargetType="Thumb">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="Height" Value="30" />
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <CompositeTransform TranslateX="10" TranslateY="10" />
                </Setter.Value>
            </Setter>
            <Setter Property="VerticalAlignment" Value="Bottom" />
            <Setter Property="Width" Value="30" />
        </Style>
    </Grid.Resources>
    <TextBox Header="First Name" Text="Jerry" />
    <Thumb DoubleTapped="GrabDoubleTapped" DragDelta="GrabDelta" Loaded="GrabLoaded" />
    <Polygon Points="0,19 19,0, 19,19" />
</Grid>

这是处理它的代码隐藏:

Windows.Foundation.Size originalSize;
private void GrabLoaded(object sender, RoutedEventArgs e)
{
    originalSize = MyTextBox.RenderSize;
}

private void GrabDelta(object sender, Windows.UI.Xaml.Controls.Primitives.DragDeltaEventArgs e)
{
    MyTextBox.Width = MyTextBox.ActualWidth + e.HorizontalChange;
    MyTextBox.Height = MyTextBox.ActualHeight + e.VerticalChange;
}

private void GrabDoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
    MyTextBox.Height = originalSize.Height;
    MyTextBox.Width = originalSize.Width;
}

您可以轻松地将其包装到控件或用户控件或其他内容中。当然。

看起来像这样:

enter image description here

祝你好运!

答案 1 :(得分:0)

艾哈迈德

我有两种解决方案。

解决方案1:

一个简单的,前期的解决方案可能是使用网格分割器,它可以帮助您拖动&amp;调整控件的大小。网格分割器的数量取决于您希望调整TextBox大小的方式。以下是代码示例:

<Grid>...
<GridSplitter Grid.Row="1" Grid.ColumnSpan="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="4" BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent"/>
<TextBox Grid.Row="2" Grid.Column="0" Margin="6,6,6,6" Name="RequestTextBox" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Text="{Binding Request, Mode=TwoWay}"/>
<GridSplitter Grid.Row="2" Grid.ColumnSpan="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="4" BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent"/>
...</Grid>

注意:您可以放置​​4个网格分割器来从4个方向调整文本框的大小,但是抬头,您可能必须在GridSplitter上定义DragCompleted事件以调整宽度和宽度。高度。

解决方案2:

你也可以用 Adorner 来做到这一点。以下是关于该主题的四部分系列,没有和有Adorners:

P.S:我还没有总结链接传达的内容,因为这个话题本身就是如此巨大。因此建议您检查链接。万一链接已经死了,谷歌搜索Adorners不应该太难;)