使用自定义 WPF 控件时无法设置某些属性

本文关键字:设置 属性 自定义 WPF 控件 | 更新日期: 2023-09-27 17:55:47

因此,我有一个名为WatermarkTextbox的自定义WPF控件,它扩展了TextBox。 我添加到代码中的唯一内容是用于保存水印文本的字符串依赖项属性。 其余的魔法在Xaml(下图)中。

<Style TargetType="wpfControls:WatermarkTextbox" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type wpfControls:WatermarkTextbox}">
                <Grid>
                    <TextBox x:Name="baseTextBox" />
                    <TextBlock Margin="5,0,0,0" x:Name="watermarkText" IsHitTestVisible="False" FontWeight="Light" FontStyle="Italic" Foreground="DarkGray" Visibility="Hidden" Background="Transparent"
                               Text="{Binding RelativeSource={RelativeSource AncestorType=wpfControls:WatermarkTextbox}, Path=Watermark}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger SourceName="baseTextBox" Property="Text" Value="">
                        <Setter TargetName="watermarkText" Property="Visibility" Value="Visible"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在我的应用程序中使用时:

<wpfControls:WatermarkTextbox Watermark="Text that disappears."/>

这在大多数情况下是有效的。 我可以设置水印文本,当我开始输入一些文本时,它会消失。 当我更改字体大小时,它会更改水印和我输入的文本;当我更改字体粗细时,它只会更改输入的文本(这就是我希望它执行的操作)。 我可以更改文本框的大小。 那都是肉汁。

问题是当我开始尝试更改文本框的背景或边框属性等内容时,如下所示。

<wpfControls:WatermarkTextbox Watermark="Text that disappears." Background="Yellow"/>

什么也没发生。 与 BorderBrush 和 BorderThick 的行为相同。 现在,我知道的部分足以知道有一些我不知道的重要概念。 如果我将水印文本框的模板更改为以下内容,它将让我根据需要在应用程序中设置背景。

<Style TargetType="wpfControls:WatermarkTextbox" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type wpfControls:WatermarkTextbox}">
                <Grid>
                    <TextBox x:Name="baseTextBox" 
                             Background="{TemplateBinding Background}"/>
                    <TextBlock Margin="5,0,0,0" x:Name="watermarkText" IsHitTestVisible="False" FontWeight="Light" FontStyle="Italic" Foreground="DarkGray"
                               Text="{Binding RelativeSource={RelativeSource AncestorType=wpfControls:WatermarkTextbox}, Path=Watermark}" Visibility="Hidden" Background="Transparent"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger SourceName="baseTextBox" Property="Text" Value="">
                        <Setter TargetName="watermarkText" Property="Visibility" Value="Visible"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我假设如果我对BorderBrush和BorderThickness做同样的事情,它们也会起作用。

所以,我的问题是,为什么? 是什么让这些属性的行为与 FontSize 和 FontWeight 或 Height and Width 不同? 为什么我必须显式将背景设置为 {模板绑定背景} 而不是字体大小? 另外,我需要为模板绑定设置哪些其他属性才能使它们正常工作?

使用自定义 WPF 控件时无法设置某些属性

某些属性会自动继承自其父属性。 解释

这就是为什么您不必设置字体大小的原因。

至于"还有什么",这完全取决于您希望能够直接在用户控件上设置的内容。

虽然这不是防弹的,但我的一般经验法则是"如果它是属性窗口Brush选项卡中的属性,或者纯粹是为了视觉美学,它可能不是继承的"

另一种看待它的方式 - 如果设置通常会给出奇怪的结果,那么它也可能不是继承的。示例:如果您在Grid上设置 Margin 属性,想象一下,如果每个子元素都继承了相同的边距。

所以我通常会为所有非布局、视觉属性(Background, Foreground, BorderBrush, etc.)添加模板绑定。或者我只是为我想直接设置为我的用户控件的任何属性添加模板绑定。如果从不打算设置属性(显式或按样式),则无需添加模板绑定。

  • 字体属性是继承的附加属性;它们是显式的特殊情况。顺便说一下,TextElement.Foreground就是其中之一。
  • 高度和宽度不是继承的,但你那里的 XAML 默认情况下将调整为其父级,并且你尚未执行任何操作来禁用该行为。

其他任何事情,你需要显式地做TemplateBinding的事情 - 或者如果它们在运行时会改变,你需要做一个相对源代码绑定:

{Binding PropertyName, RelativeSource={RelativeSource TemplatedParent}}

对此没有任何"第一原则"的解释;这只是任意的实现细节。