Question

Quelqu'un peut-il me diriger vers un exemple ou une explication qui me soit aider:

  1. Prolonger la SilverLight Box pour permettre AutoComplete filigranes.
  2. Étendre le filigrane TextBox pour permettre la fonctionnalité de saisie semi-automatique.

Il me semble que l'option 1 serait plus facile, mais je suis ouvert.

Merci d'avance.

Était-ce utile?

La solution

Dès le départ, je dirais que l'option 1 est bon:

1) Créer une propriété attachée à maintenir la WatermarkText que vous pouvez utiliser sur AutoCompleteBox.

2) Créer un modèle de contrôle pour la AutoCompleteBox (il suffit de copier celui utilisant un mélange existant), mais changer la zone de texte à un filigrane TextBox, et utiliser un TemplateBinding pour définir la propriété du WatermarkTextBox à la valeur de la propriété attachée. Le modèle de contrôle doit être appliqué dans un style (par exemple WatermarkedAutoCompleteBoxStyle).

Vous devriez être bon d'aller avec ça. Chaque fois que vous voulez une boîte de saisie semi-automatique filigrané, situé juste à la valeur de la propriété ci-joint et appliquer le style que vous avez défini.

Si vous avez besoin de plus d'une explication de ces étapes en profondeur, juste lever la main et je vais essayer de trouver le temps de créer un échantillon.

Sinon, vous pouvez tirer de AutoCompleteBox, ajoutez un DependencyProperty au lieu d'une propriété attachée et emballer le style dans les thèmes / fichier generic.xaml, mais je le fais habituellement une fois qu'il fonctionne.

Autres conseils

D'après 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" />
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top