首页 > 解决方案 > 更改 CustomControl 中子元素的样式

问题描述

我有一个CustomTextBox如下:

<TextBox x:Class="StackOverflow.CustomUserControl.CustomTextBoxView"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     x:Name="MainTextBox"
     mc:Ignorable="d">
<TextBox.Template>
    <ControlTemplate>
        <Grid>
            <ScrollViewer x:Name="PART_ContentHost"
                          Margin="0,0,0,5"
                          Panel.ZIndex="1" />

            <Border CornerRadius="2"
                    Name="BorderToChange"
                    BorderThickness="0,0,0,3.7"/>

            <TextBlock Text="{Binding ElementName=MainTextBox, Path=HintText}">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Foreground"
                                Value="Transparent" />
                        <Setter Property="Margin"
                                Value="0,0,0,3" />
                        <Setter Property="VerticalAlignment"
                                Value="Bottom" />
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </Grid>
    </ControlTemplate>
</TextBox.Template>

我使用它CustomTextBox如下:

<customUserControl:CustomTextBoxView Width="50"
     Height="50"
     HorizontalContentAlignment="Center"
     VerticalContentAlignment="Center"
     Text="{Binding Mode=TwoWay, Path=Period, UpdateSourceTrigger=PropertyChanged}"/>

有没有办法可以在使用时更改我的内部名为“BorderToChange”BorderThickness的元素的属性?我尝试了以下方法,但没有奏效:BorderControlTemplateCustomTextBox

<customUserControl:CustomTextBoxView Width="50"
                                        Height="50"
                                        HorizontalContentAlignment="Center"
                                        VerticalContentAlignment="Center"
                                        FontSize="35"
                                        Text="{Binding Mode=TwoWay, Path=Period, UpdateSourceTrigger=PropertyChanged}">
    <customUserControl:CustomTextBoxView.Resources>
        <Style TargetType="{x:Type customUserControl:CustomTextBoxView}">
            <Style.Resources>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="BorderThickness" Value="10"></Setter>
                </Style>
            </Style.Resources>
        </Style>
    </customUserControl:CustomTextBoxView.Resources>
</customUserControl:CustomTextBoxView>

标签: wpfxaml

解决方案


{TemplateBinding}在模板中使用 a :

<Border CornerRadius="2" Name="BorderToChange" BorderThickness="{TemplateBinding BorderThickness}"/>

...并指定一个默认值:

<TextBox x:Class="..." BorderThickness="0,0,0,3.7"> ...

然后,您可以简单地设置BorderThickness控件的属性来覆盖默认值:

<customUserControl:CustomTextBoxView ... BorderThickness="1" ... />

推荐阅读