如何在运行时通过用户交互调整 XAML 文本框的大小? [英] How can I resize a XAML TextBox at runtime through user interaction?
本文介绍了如何在运行时通过用户交互调整 XAML 文本框的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以在运行时和通过用户交互调整 XAML TextBox
的大小.也就是说,用户根据需要使用把手手动调整 TextBox
的大小.尽管 TextBox
似乎有一个 sizeChanged
事件,但我不知道如何在运行时手动更改大小.
I'd like to know if it's possible to resize a XAML TextBox
during runtime, and through user interaction. That is, the user uses handlebars to manually resize the TextBox
as needed. Although the TextBox
seems to have a sizeChanged
event, I can't figure out how can I possibly change the size manually at runtime.
推荐答案
这是创建效果的 XAML:
Here's the XAML to create the effect:
<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>
这是处理它的代码隐藏:
And here's the code-behind to handle it:
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;
}
您可以轻松地将其包装到控件或用户控件或其他东西中.当然.
You could easily wrap this into a control or user control or something. Sure.
看起来像这样:
祝你好运!
这篇关于如何在运行时通过用户交互调整 XAML 文本框的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文