【问题标题】:UWP Attached Property in ControlTemplateControlTemplate 中的 UWP 附加属性
【发布时间】: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 内的附加属性?!

【问题讨论】:

    标签: c# uwp uwp-xaml


    【解决方案1】:

    应该可以。如果您在模板中硬编码 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\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.?????.0\Generic\generic.xaml找到样式。

    【讨论】:

    • 我在这上面花了 8 个小时......t。昨晚的常量有效,但绑定不是(TemplateBinding 和 Binding Source={RelativeSource TemplatedParent})现在我再次运行 VS - 我上面的示例不起作用但 CornerRadius="{TemplateBinding icp:IcpExtend.CornerRadius}" 开始工作(这是我在尝试寻找其他东西之前的第一种绑定)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 2016-04-14
    • 2018-07-21
    • 2017-03-23
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多