覆盖 XAML 中的控制模板 属性
override control template property in XAML
如果我在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 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]" />
这会在 Template
中的 Border
上的 CornerRadius
为 5 的视图上显示一个文本框。
现在对于我的问题,我有一个场景,其中有两个 TextBox 彼此相邻,我想为两个 TextBox 指定 CornerRadius
,如下所示。
<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]" />
但是 TextBox 没有 CornerRadius
我怎样才能做到这一点,以便我能够更改视图 Border
的 CornerRadius
XAML
如评论中所述,您需要代理 属性,您可以针对 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
手动更改它
<TextBox local:AttachedProperties.CornerRadius="5,5,0,0" .../>
如果我在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 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]" />
这会在 Template
中的 Border
上的 CornerRadius
为 5 的视图上显示一个文本框。
现在对于我的问题,我有一个场景,其中有两个 TextBox 彼此相邻,我想为两个 TextBox 指定 CornerRadius
,如下所示。
<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]" />
但是 TextBox 没有 CornerRadius
我怎样才能做到这一点,以便我能够更改视图 Border
的 CornerRadius
XAML
如评论中所述,您需要代理 属性,您可以针对 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
<TextBox local:AttachedProperties.CornerRadius="5,5,0,0" .../>