我不了解WPF,现在正在学习。我在 WPF 中寻找圆角
TextBox
。所以我在Google上搜索并找到了一块XAML
:
<!–Rounded Corner TextBoxes–>
<ControlTemplate x:Key=”RoundTxtBoxBaseControlTemplate” TargetType=”{x:Type Control}”>
<Border Background=”{TemplateBinding Background}” x:Name=”Bd” BorderBrush=”{TemplateBinding BorderBrush}”
BorderThickness=”{TemplateBinding BorderThickness}” CornerRadius=”6″>
<ScrollViewer x:Name=”PART_ContentHost”/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property=”IsEnabled” Value=”False”>
<Setter Property=”Background” Value=”{DynamicResource {x:Static SystemColors.ControlBrushKey}}” TargetName=”Bd”/>
<Setter Property=”Foreground” Value=”{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}”/>
</Trigger>
<Trigger Property=”Width” Value=”Auto”>
<Setter Property=”MinWidth” Value=”100″/>
</Trigger>
<Trigger Property=”Height” Value=”Auto”>
<Setter Property=”MinHeight” Value=”20″/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
所以请告诉我将其粘贴到哪里
XAML
。请详细帮助我。我是 WPF 的初学者。
@Smolla 在对 @Daniel Casserly 的回答的评论中给出了更好的答案:
<TextBox Text="TextBox with CornerRadius">
<TextBox.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="3"/>
</Style>
</TextBox.Resources>
</TextBox>
如果您希望 TextBox 和 ListBox 的所有边框都有圆角,请将样式放入您的 Window 或 App 中
<Resources>
。
在 WPF 中,您可以修改或重新创建控件的外观。因此,如果您的示例他们所做的是通过修改现有
ControlTemplate
的 TextBox
来更改文本框的外观。因此,要查看和探索这段代码,只需使用下面的代码
<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
<Border Background="{TemplateBinding Background}"
x:Name="Bd" BorderBrush="Black"
BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<Trigger Property="Width" Value="Auto">
<Setter Property="MinWidth" Value="100"/>
</Trigger>
<Trigger Property="Height" Value="Auto">
<Setter Property="MinHeight" Value="20"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<TextBox Template="{StaticResource TextBoxBaseControlTemplate}" Height="25" Margin="5"></TextBox>
</Grid>
因此,我们在窗口的资源部分声明了一个静态资源,并在
Template
的 TextBox
属性中使用了 Resource TextBoxBaseControlTemplate 作为 Template="{StaticResource TextBoxBaseControlTemplate}"
。
自定义 WPF 控件的模板只需参考此文档即可获得想法
您可以使用以下样式将所有文本框更改为圆角:
<Style TargetType="{x:Type TextBox}">
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="3" />
</Style>
</Style.Resources>
</Style>
这个问题在 msdn 上得到了很好的讨论: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/549775ed-1c2a-4911-9078-d9c724294fb3/
尝试那里的解决方案,它们非常详细,并且足够详细,让您知道将代码放在哪里。
您可以使用附加属性来设置
TextBox
边框半径(同样适用于按钮)。
为附加属性创建类
public class CornerRadiusSetter
{
public static CornerRadius GetCornerRadius(DependencyObject obj) => (CornerRadius)obj.GetValue(CornerRadiusProperty);
public static void SetCornerRadius(DependencyObject obj, CornerRadius value) => obj.SetValue(CornerRadiusProperty, value);
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.RegisterAttached(nameof(Border.CornerRadius), typeof(CornerRadius),
typeof(CornerRadiusSetter), new UIPropertyMetadata(new CornerRadius(), CornerRadiusChangedCallback));
public static void CornerRadiusChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
Control control = sender as Control;
if (control == null) return;
control.Loaded -= Control_Loaded;
control.Loaded += Control_Loaded;
}
private static void Control_Loaded(object sender, EventArgs e)
{
Control control = sender as Control;
if (control == null || control.Template == null) return;
control.ApplyTemplate();
Border border = control.Template.FindName("border", control) as Border;
if (border == null) return;
border.CornerRadius = GetCornerRadius(control);
}
}
然后您可以使用附加属性语法来设置多个文本框的样式,而无需重复样式:
<TextBox local:CornerRadiusSetter.CornerRadius="10" />
<TextBox local:CornerRadiusSetter.CornerRadius="5, 0, 0, 5" />
<TextBox local:CornerRadiusSetter.CornerRadius="10, 4, 18, 7" />
以困难的方式去做。
在 Visual Studio 中创建一个新的空 WPF 项目。将
TextBox
添加到主 Window
。然后将鼠标放在 TextBox
上,右键单击并选择 编辑模板、编辑副本...。在出现的对话框中,选择应用于全部和此文档。
您现在拥有
TextBox
模板的副本。现在看看名称为 Border
的 border
。只需添加一个 CornerRadius
即可。
接下来将此代码复制/粘贴到您的
App.xaml
中的/Application/Application.Resources/ResourceDictionary
中。
它比其他解决方案需要更多的时间,它更复杂,但更干净,一旦你掌握了这个过程,你就可以使用 WPF 做任何你想做的事情。
这读起来很有趣。