【发布时间】:2016-10-27 06:23:08
【问题描述】:
我正在尝试扩展 TextBox 默认样式 https://msdn.microsoft.com/en-us/library/windows/apps/mt299154.aspx
我想让文本框的角变圆。
<Border
x:Name="BackgroundElement"
Grid.Row="1"
Background="{TemplateBinding Background}"
Margin="{TemplateBinding BorderThickness}"
CornerRadius="{Binding Source={RelativeSource TemplatedParent}, Path=(icp:IcpExtend.CornerRadius)}"
Opacity="{ThemeResource TextControlBackgroundRestOpacity}"
Grid.ColumnSpan="2"
Grid.Column="0" />
<Border
x:Name="BorderElement"
Grid.Column="0"
Grid.Row="1"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{Binding Source={RelativeSource TemplatedParent}, Path=(icp:IcpExtend.CornerRadius)}"
Grid.ColumnSpan="2" />
这是附加的属性类
[Bindable]
public class IcpExtend
{
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
"CornerRadius", typeof(CornerRadius), typeof(IcpExtend), new PropertyMetadata(default(CornerRadius)));
public static void SetCornerRadius(DependencyObject element, CornerRadius value)
{
element.SetValue(CornerRadiusProperty, value);
}
public static CornerRadius GetCornerRadius(DependencyObject element)
{
return (CornerRadius) element.GetValue(CornerRadiusProperty);
}
}
这是页面上的 TextBox 元素
<TextBox
icp:IcpExtend.CornerRadius="10"
Grid.Row="0"
Grid.Column="1"
PlaceholderText="Email"
InputScope="EmailSmtpAddress"
IsSpellCheckEnabled="False"
Text="{Binding Path=Email, Mode=TwoWay}" />
解决方案不起作用(角不是圆角)...如何绑定到 Default Style -> ControlTemplate 内的附加属性?!
【问题讨论】: