C# 如何在运行时通过用户交互调整XAML文本框的大小?
我想知道是否可以在运行时通过用户交互调整XAMLC# 如何在运行时通过用户交互调整XAML文本框的大小?,c#,xaml,textbox,windows-runtime,winrt-xaml,C#,Xaml,Textbox,Windows Runtime,Winrt Xaml,我想知道是否可以在运行时通过用户交互调整XAMLTextBox。也就是说,用户使用手柄根据需要手动调整文本框的大小。虽然文本框似乎有一个sizeChanged事件,但我不知道如何在运行时手动更改大小。Ahmad 我有两个解决方案 解决方案1: 一个简单的预先解决方案是使用网格拆分器,它可以帮助您拖动和调整控件的大小。网格拆分器的数量取决于您希望调整文本框大小的方式。下面是一个代码示例: <Grid>... <GridSplitter Grid.Row="1" Grid.Colu
TextBox
。也就是说,用户使用手柄根据需要手动调整文本框的大小。虽然文本框
似乎有一个sizeChanged
事件,但我不知道如何在运行时手动更改大小。Ahmad
我有两个解决方案
解决方案1:
一个简单的预先解决方案是使用网格拆分器,它可以帮助您拖动和调整控件的大小。网格拆分器的数量取决于您希望调整文本框大小的方式。下面是一个代码示例:
<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个方向调整文本框的大小,但是平视,您可能需要在网格拆分器上定义DragCompleted事件来调整宽度和高度
解决方案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>
您可以轻松地将其包装到控件或用户控件或其他内容中。当然
看起来像这样:
祝你好运 只有WPF有装饰物,而不是WinRT。@WiredPairie:我的错。网格拆分器解决方案会有帮助吗?它也不能解决原始请求。只有在最简单的UI中才能工作。为什么不能在运行时设置宽度和高度?通常,标记正确答案被认为是礼貌的。
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;
}