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

时间:2016-07-06 16:06:36

标签: c# wpf xaml custom-controls

因此,我有一个名为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和BorderThickness相同的行为。现在,我知道的那部分足以让我知道一些我不知道的重要概念。如果我将WatermarkTextbox的模板更改为以下内容,它将让我在我的应用程序中设置背景,就像我想要的那样。

<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和Width的行为有何不同?为什么我必须将背景显式设置为{TemplateBinding Background}而不是FontSize?另外,我需要将哪些其他属性设置为TemplateBinding才能使它们正常工作?

2 个答案:

答案 0 :(得分:3)

其他任何事情,你需要明确地做TemplateBinding事情 - 或者如果他们在运行时改变,你需要做一个亲属资源绑定:

{Binding PropertyName, RelativeSource={RelativeSource TemplatedParent}}

对此没有任何“第一原则”的解释;它只是随意的实现细节。

答案 1 :(得分:3)

某些属性会自动从其父级继承。 explanation

这就是您不必设置FontSize的原因。

至于“还有什么”,这一切都取决于您希望能够直接在用户控件上设置的内容。

虽然这不是防弹,但我的一般经验法则是“如果它是属性窗口的Brush标签中的属性,或纯粹是为了视觉美学,​​它可能不是继承的”< / p>

另一种看待它的方法 - 如果设置一般会产生奇怪的结果,它也可能没有继承。示例:如果在Margin上设置Grid属性,请想象每个子元素是否继承了相同的边距。

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