Sur la base de la réponse de Steve:
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
Le style:
<!-- 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>
Utilisation:
<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" />
LOL ... OK, main officiellement levé. Vous n'avez pas besoin de créer un exemple, mais si vous pouvez pointer vers un bon exemple de # 1. Je n'ai pas encore créé de propriété jointe. Merci! –
En fait, j'ai un exemple d'utilisation d'une propriété jointe pour faire quelque chose de similaire (pas tout à fait, mais il devrait vous montrer la syntaxe) sur http://denis-troller.blogspot.com/ (ce n'est pas une prise, il n'y a en fait que article là-bas :)) –
Denis. Merci beaucoup. Appréciez l'info. –