【发布时间】:2016-09-01 01:33:29
【问题描述】:
我想创建自定义文本框(其中将包含简单的水印/提示文本)并且我遇到了一些依赖属性的问题。 我的自定义类继承自 TextBox 类并包含两个文件 (.xaml .cs) C#代码文件很简单:
public partial class HintTextBox : TextBox
{
public string HintText
{
get { return (string)GetValue(HintTextProperty); }
set { SetValue(HintTextProperty, value); }
}
public static readonly DependencyProperty HintTextProperty =
DependencyProperty.Register(
"HintText",
typeof(string),
typeof(HintTextBox),
new FrameworkPropertyMetadata(string.Empty));
}
.xaml 文件包含显示/隐藏水印的代码
<local:HintTextBox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Ribes.Client.GUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<local:HintTextBox.Style>
<Style TargetType="{x:Type local:HintTextBox}">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{Binding HintText, ElementName=local:HintTextBox}" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers><!-- triggers changing content of control --></Style.Triggers>
</Style>
</local:HintTextBox.Style>
我正在尝试简单地使用我的 HintTextBox:
<local:HintTextBox HintText="hint text"></local:HintTextBox>
而且它不起作用。 在线创建刹车点后:
SetValue(HintTextProperty, value);
代码永远无法到达。 TextBox 的继承属性(文本、背景)工作正常:
<local:HintTextBox Text="example text" Background="Yellow"></local:HintTextBox>
我们可以看到正确的黄色文本框,里面有“示例文本”。
有人知道我的 DP 有什么问题吗?
问候 NQ
【问题讨论】:
-
如果我没记错的话,WPF 不使用 DP 的包装器属性,它使用带有 DP 的
SetValue- 这就是没有达到断点的原因。我怀疑,绑定({Binding HintText, ElementName=local:HintTextBox})不正确(我的意思是ElementName)