UWP 在 ControlTemplate 中附加 属性
UWP Attached Property in ControlTemplate
我正在尝试扩展 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" />
附上属性class
[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}" />
解决方案不工作(角不圆)...如何绑定到默认样式 -> ControlTemplate 中附加的 属性?!
应该可以。如果您在模板中硬编码 CornerRadius,它会起作用吗?如果不是,那么你的风格有问题。您应该将 TextBox 样式放入 App.xaml 资源中,该样式应该只有 TargetType="TextBox"
而没有键。
此外,您应该能够在模板中使用 TemplateBinding 而不是 RelativeSource:
CornerRadius="{TemplateBinding icp:IcpExtend.CornerRadius}"
另一件要记住的事情是,这种风格适用于 Windows SDK 10586(如该页面所述)。我使用的是 SDK 14393,TextBox 控件的默认样式与 SDK 10586 中的不同。请确保您使用的是正确的样式。您可以在 C:\Program Files (x86)\Windows Kits\DesignTime\CommonConfiguration\Neutral\UAP.0.?????.0\Generic\generic.xaml
.
中找到样式
我正在尝试扩展 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" />
附上属性class
[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}" />
解决方案不工作(角不圆)...如何绑定到默认样式 -> ControlTemplate 中附加的 属性?!
应该可以。如果您在模板中硬编码 CornerRadius,它会起作用吗?如果不是,那么你的风格有问题。您应该将 TextBox 样式放入 App.xaml 资源中,该样式应该只有 TargetType="TextBox"
而没有键。
此外,您应该能够在模板中使用 TemplateBinding 而不是 RelativeSource:
CornerRadius="{TemplateBinding icp:IcpExtend.CornerRadius}"
另一件要记住的事情是,这种风格适用于 Windows SDK 10586(如该页面所述)。我使用的是 SDK 14393,TextBox 控件的默认样式与 SDK 10586 中的不同。请确保您使用的是正确的样式。您可以在 C:\Program Files (x86)\Windows Kits\DesignTime\CommonConfiguration\Neutral\UAP.0.?????.0\Generic\generic.xaml
.