如何在Winrt中将PasswordMark属性添加到PasswordBox?

时间:2013-07-10 12:41:39

标签: textbox windows-runtime watermark maskedtextbox passwordbox

我需要WatermarkPasswordBox控件,但Winrt中没有。也许我们可以在PasswordBox中添加Watermark属性。是否有人可以做到这一点?

由于

3 个答案:

答案 0 :(得分:2)

水印的目的是传达控件背后的信息。在此演示的情况下,在您开始键入文本后,水印也会消失,因此它们更像是一个字段“提示”,告诉​​您预期的内容。

为实现这一目标,我们转向常规的WPF解决方案提供商AttachedProperty。 AttachedProperties允许您为任何控件添加额外的属性。您还可以将其扩展为Attachedbehaviour,您可以在其中控制对房产变更做出反应。

在此示例中,我们使用两个附加属性。第一个“WaterrmarkProperty”获取水印值并初始化控件:

public static string GetWatermark(DependencyObject obj) 
{ 
    return (string)obj.GetValue(WatermarkProperty); 
} 

public static void SetWatermark(DependencyObject obj, string value) 
{ 
    obj.SetValue(WatermarkProperty, value); 
} 

public static readonly DependencyProperty WatermarkProperty = 
    DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(TextBoxHelper), new UIPropertyMetadata(null, WatermarkChanged));

第二个附加属性是通知框中是否有值,模板绑定并隐藏或显示水印。

public static bool GetShowWatermark(DependencyObject obj) 
{ 
    return (bool)obj.GetValue(ShowWatermarkProperty); 
} 

public static void SetShowWatermark(DependencyObject obj, bool value) 
{ 
    obj.SetValue(ShowWatermarkProperty, value); 
} 

public static readonly DependencyProperty ShowWatermarkProperty = 
    DependencyProperty.RegisterAttached("ShowWatermark", typeof(bool), typeof(TextBoxHelper), new UIPropertyMetadata(false));

对于TextBoxHelper,无论何时更改文本,水印都会显示或隐藏如下:

private static void CheckShowWatermark(TextBox box) 
{ 
    box.SetValue(TextBoxHelper.ShowWatermarkProperty, box.Text == string.Empty); 
} 

这由ControlTemplate:

控制
<ControlTemplate x:Key="WatermarkedTextBoxTemplate" TargetType="{x:Type TextBox}"> 
    <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true"> 
        <Grid> 
            <TextBlock Text="{Binding Path=(local:TextBoxHelper.Watermark), RelativeSource={RelativeSource TemplatedParent}}" Opacity=".5" FontWeight="Bold" Visibility="{Binding (local:TextBoxHelper.ShowWatermark), Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}" /> 
            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Grid> 
    </Microsoft_Windows_Themes:ListBoxChrome> 
    <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="false"> 
            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate>

来源:http://code.msdn.microsoft.com/windowsdesktop/Watermarked-TextBox-and-444ebdec

答案 1 :(得分:2)

在Windows 8.0中,您可以使用WinRT XAML工具包中的WatermarkPasswordBox,您可以从here获取该工具包。它为您提供了Watermark属性,可将任何UI元素(ShapeImage等)设置为带有文本和{{1}的水印或WatermarkText属性需要WatermarkTextStyle TextBlock来设置文字样式。

在Windows 8.1中,您可以使用相同或使用新的PlaceholderText属性。

随意从库中删除并修改Style控件的代码,并在您的应用中使用它。这是麻省理工学院的许可。不需要积分。只需获取.cs和.xaml文件,并在你的Themes / Generic.xaml中包含.xaml资源字典,如下所示:

WatermarkPasswordBox

答案 2 :(得分:1)

更新1

如果您不想使用第三方DLL,请在PasswordBoxBehavior.cs文件中添加这两种方法。

using System.Reflection;

public static T FindVisualChildByName<T>(this DependencyObject fe, string name) where T : DependencyObject
{
    if (string.IsNullOrEmpty(name))
    {
        throw new ArgumentNullException("name");
    }
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(fe); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(fe, i);
        string a = child.GetValue(FrameworkElement.NameProperty) as string;
        if (a == name)
        {
            return child as T;
        }
        T t = FindVisualChildByName<T>(child, name);
        if (t != null)
        {
            return t;
        }
    }
    return default(T);
}

public static T FindVisualParent<T>(this DependencyObject fe) where T : DependencyObject
{
    for (fe = VisualTreeHelper.GetParent(fe); fe != null; fe = VisualTreeHelper.GetParent(fe))
    {
        T t = fe as T;
        if (t != null)
        {
            return t;
        }
    }
    return default(T);
}

这是来自JulMar的广泛博客

Adding a watermark to a PasswordBox in a Windows Store app

Here’s the code if you’d like to use it yourself.

相关问题