有人可以指导我一个可以帮助我的例子或解释:
让我觉得选项1最容易,但我是开放的。
提前致谢。
答案 0 :(得分:5)
马上,我会说选项1很好:
1)创建一个附加属性来保存可以在AutoCompleteBox上使用的WatermarkText。
2)为AutoCompleteBox创建一个控件模板(只需使用blend复制现有的模板),但将TextBox更改为Watermark TextBox,并使用TemplateBinding将WatermarkTextBox的属性设置为附加属性的值。 控件模板应该以样式应用(例如WatermarkedAutoCompleteBoxStyle)。
你应该乐于助人。 只要您需要带水印的自动填充框,只需设置附加的属性值并应用您定义的样式。
如果您需要对其中一个步骤进行更深入的解释,请举手,我将尝试找到创建样本的时间。
或者,您可以从AutoCompleteBox派生,添加DependencyProperty而不是附加属性,并将样式打包在Themes / generic.xaml文件中,但我通常会在它工作后执行此操作。
答案 1 :(得分:0)
根据史蒂夫的回答:
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
风格:
<!-- input:AutoCompleteBox -->
<Style TargetType="input:AutoCompleteBox">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="input:AutoCompleteBox">
<Grid Opacity="{TemplateBinding Opacity}">
<extk:WatermarkTextBox
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
IsTabStop="True"
x:Name="Text"
Style="{TemplateBinding TextBoxStyle}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Foreground="{TemplateBinding Foreground}"
Margin="0"
Watermark="{Binding Path=(local:WatermarkExtender.Watermark), Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
WatermarkTemplate="{Binding Path=(local:WatermarkExtender.WatermarkTemplate), Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
用法:
<Window.Resources>
<Style x:Key="acWatermarkStyle" TargetType="{x:Type wtk:AutoCompleteBox}" BasedOn="{StaticResource {x:Type wtk:AutoCompleteBox}}">
<Setter Property="local:WatermarkExtender.WatermarkTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Foreground="Gray" Margin="3,0,0,0" Text="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<wtk:AutoCompleteBox
Height="25"
Margin="2"
Style="{StaticResource acWatermarkStyle}"
HorizontalAlignment="Stretch"
ValueMemberPath="SomeProp"
FilterMode="Custom"
local:WatermarkExtender.Watermark="type something" />