覆盖XAML中的控件模板属性

时间:2015-09-07 19:36:24

标签: wpf xaml cornerradius

如果我在generic.xaml中定义了以下XAML

<Style TargetType="{x:Type TextBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="CaretBrush" Value="{StaticResource TextBoxCaretBrush}" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="MinWidth" Value="120" />
    <Setter Property="MinHeight" Value="20" />
    <Setter Property="AllowDrop" Value="True" />
    <Setter Property="Foreground" Value="{StaticResource TextBoxTextBrush}" />
    <Setter Property="Padding" Value="5,3,0,5" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border x:Name="Border" CornerRadius="5" Padding="2" BorderThickness="1">
                    <Border.Background>
                        <SolidColorBrush Color="{StaticResource TextBoxBackgroundColor}" />
                    </Border.Background>
                    <Border.BorderBrush>
                        <SolidColorBrush Color="{StaticResource TextBoxBorderColor}" />
                    </Border.BorderBrush>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlLightColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="ReadOnly">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlDarkColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="BorderBrush" TargetName="Border" Value="{StaticResource TextBoxBorderSelectedBrush}" />
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect ShadowDepth="0" Color="{StaticResource HighlightColor}" Opacity="1" BlurRadius="5" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderThickness" Value="2" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在我看来,我已经定义了以下XAML来使用上面的样式显示TextBox。

<TextBox Name="UserName" attachedProperties:KeyboardNavigationExt.TabOnEnter="True" Grid.Row="1" KeyboardNavigation.TabIndex="1" Grid.Column="2" Grid.ColumnSpan="3" cal:Message.Attach="[Event TextChanged] = [Action CanLogin]" />

这会在CornerRadius Border上的TemplateCornerRadius的视图上显示一个TextBox。

现在我的问题是,我有一个场景,其中有两个TextBox彼此相邻,我想为两个TextBox指定<TextBox Name="UserName" CornerRadius="5,5,0,0" attachedProperties:KeyboardNavigationExt.TabOnEnter="True" Grid.Row="1" KeyboardNavigation.TabIndex="1" Grid.Column="2" Grid.ColumnSpan="3" cal:Message.Attach="[Event TextChanged] = [Action CanLogin]" /> ,如下所示。

CornerRadius

但是TextBox没有CornerRadius我如何实现这一点,以便我能够在视图XAML中更改Border的{​​{1}}

1 个答案:

答案 0 :(得分:4)

如评论中所述,您需要可以针对#define QDC_ALL_PATHS 0x00000001 #define QDC_ONLY_ACTIVE_PATHS 0x00000002 #define QDC_DATABASE_CURRENT 0x00000004 设置并在模板中绑定的代理属性。因此,请创建TextBox类型的AttachedProprty

CornerRadius

更改public static class AttachedProperties { public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(AttachedProperties), new UIPropertyMetadata()); public static void SetCornerRadius(DependencyObject d, CornerRadius source) { d.SetValue(CornerRadiusProperty, source); } public static CornerRadius GetCornerRadius(DependencyObject d) { return (CornerRadius)d.GetValue(CornerRadiusProperty); } } 并绑定到ControlTemplate

的该属性
TemplatedParent

使用默认值

<ControlTemplate TargetType="{x:Type TextBox}"> <Border CornerRadius="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:AttachedProperties.CornerRadius)}"/> </ControlTemplate> 添加到Setter
Style

然后您可以针对每个<Setter Property="local:AttachedProperties.CornerRadius" Value="5"/>

手动更改它
TextBox