誰かが私に役立つ例や説明を教えてもらえますか:
- SilverLight オートコンプリート ボックスを拡張して透かしを許可します。
- Watermark TextBox を拡張して、オートコンプリート機能を許可します。
オプション1が最も簡単だと思いますが、私はオープンです。
前もって感謝します。
誰かが私に役立つ例や説明を教えてもらえますか:
オプション1が最も簡単だと思いますが、私はオープンです。
前もって感謝します。
すぐに、オプション1が良いと思います:
1) AutoCompleteBox で使用できる WatermarkText を保持する添付プロパティを作成します。
2) AutoCompleteBox のコントロール テンプレートを作成します (blend を使用して既存のテンプレートをコピーするだけです)。ただし、TextBox を Watermark TextBox に変更し、TemplateBinding を使用して WatermarkTextBox のプロパティを添付プロパティの値に設定します。コントロール テンプレートは、スタイル (WatermarkedAutoCompleteBoxStyle など) で適用する必要があります。
あなたはそれでうまくいくはずです。透かし入りのオートコンプリート ボックスが必要なときはいつでも、添付プロパティの値を設定し、定義したスタイルを適用するだけです。
これらの手順の 1 つについてさらに詳しい説明が必要な場合は、手を挙げてください。時間を見つけてサンプルを作成します。
別の方法として、AutoCompleteBox から派生させ、添付プロパティの代わりに DependencyProperty を追加し、スタイルを Themes/generic.xaml ファイルにパッケージ化することもできますが、私は通常、それが機能したらそれを行います。
スティーブの答えに基づく:
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" />