gpt4 book ai didi

c# - 如何在 WPF 中为自定义 TextBox 控件指定 CornerRadius?

转载 作者:行者123 更新时间:2023-11-30 19:46:35 30 4
gpt4 key购买 nike

我使用以下代码创建了自定义 ctextbox。但我无法为此提供圆角边框。

public class FilteredTextBox : TextBox
{


public FilteredTextBox()
: base()
{
IsNumeric = false;
IsRegex = false;
IsRequired = false;
ErrorMsg = "";
RegexText = "";
HorizontalAlignment = HorizontalAlignment.Stretch;
Margin = new Thickness(0);
BorderThickness = new Thickness(1);
var border = new Border {CornerRadius = new CornerRadius(4)};
}
}

请指导我做这件事?

最佳答案

您可以使用自定义 TextBox 的样式来执行此操作:

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>

<Grid.Resources>
<Style x:Key="CustomTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border
CornerRadius="4"
Padding="2"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>

<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<CustomTextBox Style="{StaticResource CustomTextBoxStyle}" Text="TextBox with CornerRadius" BorderBrush="Black" />
</Grid>

</Grid>
</Page>

希望对你有帮助

关于c# - 如何在 WPF 中为自定义 TextBox 控件指定 CornerRadius?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370130/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com