Public Class WatermarkExtender
Inherits DependencyObject
Public Shared ReadOnly WatermarkProperty As DependencyProperty =
DependencyProperty.RegisterAttached(
"Watermark",
GetType(Object),
GetType(WatermarkExtender),
New UIPropertyMetadata(Nothing))
Public Shared ReadOnly WatermarkTemplateProperty As DependencyProperty =
DependencyProperty.RegisterAttached(
"WatermarkTemplate",
GetType(DataTemplate),
GetType(WatermarkExtender),
New UIPropertyMetadata(Nothing))
Public Shared Sub SetWatermark(ByVal element As UIElement, ByVal value As Object)
element.SetValue(WatermarkProperty, value)
End Sub
Public Shared Function GetWatermark(ByVal element As UIElement) As Object
Return element.GetValue(WatermarkProperty)
End Function
Public Shared Sub SetWatermarkTemplate(ByVal element As UIElement, ByVal value As Object)
element.SetValue(WatermarkTemplateProperty, value)
End Sub
Public Shared Function GetWatermarkTemplate(ByVal element As UIElement) As Object
Return element.GetValue(WatermarkTemplateProperty)
End Function
End Class
2 回答
马上,我会说选项1很好:
1)创建一个附加属性来保存可以在AutoCompleteBox上使用的WatermarkText .
2)为AutoCompleteBox创建一个控件模板(只需使用blend复制现有的模板),但将TextBox更改为Watermark TextBox,并使用TemplateBinding将WatermarkTextBox的属性设置为附加属性的值 . 控件模板应以样式应用(例如WatermarkedAutoCompleteBoxStyle) .
你应该乐于助人 . 只要您想要带水印的自动填充框,只需设置附加的属性值并应用您定义的样式即可 .
如果您需要更深入地解释其中一个步骤,只需举手,我将尝试找到创建样本的时间 .
或者,您可以从AutoCompleteBox派生,添加DependencyProperty而不是附加属性,并将样式打包在Themes / generic.xaml文件中,但我通常在它工作后执行此操作 .
基于史蒂夫的回答:
风格:
用法: