C# 调整文本框大小,以便在调整控件大小时正确显示字体
我想设计一个文本框的样式,这样当我的控件的大小改变时,文本框和/或字体的大小就会调整,以便正确显示 我几乎让它工作了 我的风格:C# 调整文本框大小,以便在调整控件大小时正确显示字体,c#,wpf,xaml,styles,viewbox,C#,Wpf,Xaml,Styles,Viewbox,我想设计一个文本框的样式,这样当我的控件的大小改变时,文本框和/或字体的大小就会调整,以便正确显示 我几乎让它工作了 我的风格: <UserControl.Resources> <Style x:Key="ViewBoxTextBox" TargetType="TextBox"> <Setter Property="Template"> <Setter.Value> &
<UserControl.Resources>
<Style x:Key="ViewBoxTextBox" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Viewbox HorizontalAlignment="Left">
<TextBox Text="{TemplateBinding Text}" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}"/>
</Viewbox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
示例代码:
<TextBox Style="{StaticResource ViewBoxTextBox}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="2" Text="{Binding MyText}"/>
下面是3种情况的屏幕截图
在第三个屏幕截图中,当我减小控件的高度时,文本框字体的大小正确地减小了,但是文本框的宽度减小了,并且没有占据整个可用宽度
有什么想法吗?有时候可以对控件的模板进行基本覆盖,但在这种情况下,您需要对文本框中的文本进行更细粒度的控制。为此,我得到了TextBox默认模板的副本,然后进行了一些修改
<SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
<SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
<SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>
<Style x:Key="ViewBoxTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
VerticalAlignment="Top"
Width="{Binding RelativeSource={RelativeSource TemplatedParent},Path=ActualWidth}"
SnapsToDevicePixels="True">
<Viewbox HorizontalAlignment="Left" StretchDirection="DownOnly">
<ScrollViewer x:Name="PART_ContentHost"
Focusable="false"
HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"
Width="{Binding RelativeSource={RelativeSource TemplatedParent},Path=ActualWidth}"/>
</Viewbox>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
ContentHost现在被包装在一个可视框中,该可视框将正确缩放文本,边框将根据更正常的大小约束进行缩放。我在几个不同的场景下尝试了它,它对我尝试的案例都有效。希望它适合您的使用。尝试将ViewBox的Stretch属性设置为Fill。@mm8确实可以将宽度固定到最大值,但是它阻止了文本框内的字体大小减小,而只是在高度上被压扁。最简单的方法是在
ViewBox中将其拍打。