Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight水印自动完成框_C#_Silverlight_Autocomplete_Watermark - Fatal编程技术网

C# Silverlight水印自动完成框

C# Silverlight水印自动完成框,c#,silverlight,autocomplete,watermark,C#,Silverlight,Autocomplete,Watermark,有人能给我举一个例子或解释来帮助我吗 扩展SilverLight自动完成框以允许水印 扩展水印文本框以允许自动完成功能 我觉得选择1是最简单的,但我是开放的 提前谢谢。马上,我会说选项1很好: 1) 创建一个附加属性以保存可在AutoCompleteBox上使用的水印文本 2) 为AutoCompleteBox创建控件模板(只需使用blend复制现有模板),但将文本框更改为水印文本框,并使用TemplateBinding将水印文本框的属性设置为附加属性的值。 控件模板应应用于样式中(例如水印Au

有人能给我举一个例子或解释来帮助我吗

  • 扩展SilverLight自动完成框以允许水印
  • 扩展水印文本框以允许自动完成功能
  • 我觉得选择1是最简单的,但我是开放的


    提前谢谢。

    马上,我会说选项1很好:

    1) 创建一个附加属性以保存可在AutoCompleteBox上使用的水印文本

    2) 为AutoCompleteBox创建控件模板(只需使用blend复制现有模板),但将文本框更改为水印文本框,并使用TemplateBinding将水印文本框的属性设置为附加属性的值。 控件模板应应用于样式中(例如水印AutoCompleteBoxStyle)

    你应该很乐意这样做。 任何时候需要带水印的自动完成框时,只需设置附加的特性值并应用定义的样式

    如果您需要对这些步骤中的一个进行更深入的解释,请举手,我将设法抽出时间创建一个示例


    或者,您可以从AutoCompleteBox派生,添加DependencyProperty而不是附加属性,并将样式打包到Themes/generic.xaml文件中,但我通常在它工作后执行此操作。

    基于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
    
    风格:

    <!--  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" />
    
    
    
    LOL……好的,正式举手。你不必创建一个例子,但如果你能指出#1的一个好例子。我还没有实际创建一个附加属性。谢谢事实上,我有一个例子,在Denis(这不是一个插件,实际上只有那篇文章:))使用一个附加属性来做类似的事情(不完全是,但它应该向您展示语法)。非常感谢。感谢您提供的信息。如果这还不足以满足您的需要,请告诉我您是否也可以共享WatermarkTextBox的代码?您好,WatermarkTextBox是扩展WPF工具包的一部分:。