【问题标题】:Unable to set some properties when using custom WPF Control使用自定义 WPF 控件时无法设置某些属性
【发布时间】:2016-11-08 18:41:07
【问题描述】:

所以,我有一个名为 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 设置哪些其他属性才能使其正常工作?

【问题讨论】:

    标签: c# wpf xaml custom-controls


    【解决方案1】:

    除此之外,您需要明确地执行 TemplateBinding 事情——或者如果它们会在运行时发生变化,您需要执行相对源绑定:

    {Binding PropertyName, RelativeSource={RelativeSource TemplatedParent}}
    

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

    【讨论】:

      【解决方案2】:

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

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

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

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

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

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

      【讨论】:

      • 说得很好:“如果设置通常会给出奇怪的结果,它也可能没有继承。例如:如果您在 Grid 上设置 Margin 属性,想象如果每个子元素都继承了相同的边距。”
      猜你喜欢
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多