子控件未继承 WPF CustomControl 样式

WPF CustomControl style not inherited by Child Control

我正在尝试学习如何制作自定义控件。我一直在搜索整个下午,但无法让子控件使用我的属性。

例如,我的 TextBox 子类是 TextBoxCalculator。在控件模板中,我有一个文本框和一个弹出窗口。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Controls">

    <Style TargetType="local:TextBoxCalculator" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:TextBoxCalculator">

                    <Grid Name="ucGrid">
                        <TextBox Name="AmountTb" Text="{Binding Text }">
                            <!--<TextBox.Style>
                                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                                ... How can I make this textbox style follow the one specified in the window XAML?
                                </Style>
                            </TextBox.Style>-->
                        </TextBox>
                        <Popup Name="calPopup"  Placement="Top" StaysOpen="False">
                          ... some content
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

在我的 window 中,我使用这样的控件:

    <Style TargetType="{x:Type controls1:TextBoxCalculator}" x:Key="RightCellEditCalc">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="TextAlignment" Value="Right"/>
        <Setter Property="Background" Value="Red"/>
        <Setter Property="BorderThickness" Value="0"/>
    </Style>

<controls1:TextBoxCalculator Text="{Binding Amount, UpdateSourceTrigger=LostFocus}" 
                        DecimalPlaces="2"
                        AutoPlaceDecimalPoint="True"
                        Style="{StaticResource RightCellEditCalc}"
                        Background="Green">
</controls1:TextBoxCalculator>

我希望背景是绿色的,但实际上是白色的。 另一方面,TextAlignment = Right 似乎有效。我错过了什么?

您没有将 background.binding 绑定到 ControlTemplate.For 示例中您想要的位置

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Controls">

<Style TargetType="local:TextBoxCalculator" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TextBoxCalculator">

                <Grid Name="ucGrid">
                    <TextBox Name="AmountTb" Text="{Binding Text }" Background="{TemplateBinding Background}">
                        <!--<TextBox.Style>
                            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                            ... How can I make this textbox style follow the one specified in the window XAML?
                            </Style>
                        </TextBox.Style>-->
                    </TextBox>
                    <Popup Name="calPopup"  Placement="Top" StaysOpen="False">
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

你现在可以使用背景了。

在另一个自定义 TextBox 控件的 ControlTemplate 中定义 TextBox 元素毫无意义。

如果您想为您的控件创建一个自定义模板并将其基于 TextBox 控件的默认模板,您可以在设计模式下右键单击 TextBox 元素 Visual Studio 或在 Blend 中选择编辑模板->编辑副本将默认模板复制到您的 XAML 标记中,然后根据您的要求进行编辑。

这是 Windows 8 及更高版本上的样子:

<ControlTemplate TargetType="{x:Type TextBox}">
    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="true">
            <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

它使用 TemplateBindings 将模板中 Border 元素的属性绑定到 TextBox 控件本身的相应 属性。