0

I have a passwordbox implemented as a custom control as show below. I am having difficult adding validation which needs to check if the password is empty. If it is then i would need to display a message stating "Please enter a password"

   <UserControl x:Class="Controls.MyPasswordBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d">
        <UserControl.Resources>

    </UserControl.Resources>
    <PasswordBox Name="MyPasswordBoxControl" 
                 DataContext="{Binding Path=MyPasswordBoxControl, Mode=TwoWay}"></PasswordBox>

My code behind is as follows:

using System.Windows;
using System.Windows.Controls;

namespace Client.Controls
{

    public partial class MyPasswordBox : UserControl
    {
        public MyPasswordBox()
        {
            InitializeComponent();

            MyPasswordBoxControl.PasswordChanged += delegate
            {
                Value = MyPasswordBoxControl.Password;
            };

            if (!MyPasswordBoxControl.IsLoaded)
            {
                MyPasswordBoxControl.Loaded += delegate
                {
                    MyPasswordBoxControl.Password = Value;
                };
            }
        }

        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(MyPasswordBox), new PropertyMetadata(""));
    }
}

How can i add validation which will check if the password box is empty or not?

Thanks in advance

tjhack
  • 1,022
  • 3
  • 20
  • 43
  • I think you are looking for a [watermark](http://stackoverflow.com/questions/833943/watermark-hint-text-placeholder-textbox-in-wpf) – default Jan 30 '15 at 15:49

2 Answers2

0

Unless you are adding more functionality to MyPasswordBox, you could just use a PasswordBox directly, then add your validation to a button click handler or whatever event is most appropriate for your application.

For example, in the xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <PasswordBox Grid.Row="0" Name="MyPasswordBox" ></PasswordBox>
    <Button Grid.Row="1" Name="OkButton" Click="OkButton_Click">OK</Button>        
</Grid>

Then, in the code behind:

private void OkButton_Click(object sender, RoutedEventArgs e)
    {
        if (MyPasswordBox.Password == String.Empty)
        {
            MessageBox.Show("Please enter a password");
        }
    }

To show a tooltip when the password box is empty, you could do this sort of thing with the xaml:

<PasswordBox Grid.Row="0" Name="MyPasswordBox" ToolTip="Please enter a password"  PasswordChanged="MyPasswordBox_PasswordChanged"></PasswordBox>

Then in the code behind:

private void MyPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        ToolTipService.SetIsEnabled(MyPasswordBox, MyPasswordBox.Password == String.Empty);
    }

To have the error appear to the right of the PasswordBox, you could do something like this:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <PasswordBox Grid.Row="0" Name="MyPasswordBox" Margin="5" PasswordChanged="MyPasswordBox_PasswordChanged"></PasswordBox>
        <Label Grid.Row="0" Grid.Column="1" Name="EnterPasswordLabel">Please enter a password</Label>
        <Button Grid.Row="1" Grid.ColumnSpan="2" Name="OkButton" Click="OkButton_Click">OK</Button>

    </Grid>

Then in the code behind:

private void MyPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        EnterPasswordLabel.Visibility = MyPasswordBox.Password == String.Empty ? Visibility.Visible : Visibility.Collapsed;  
    }
Gary Wright
  • 2,441
  • 1
  • 20
  • 32
  • Thanks for your reply. I need to bind the validation to the password box and display a tootip style error message. – tjhack Feb 02 '15 at 12:49
  • I'm not sure I fully understand the requirement, but I've amended the answer to include a tooltip error message. Is that the sort of thing you need? – Gary Wright Feb 02 '15 at 13:05
  • More like a message on the side which states the error – tjhack Feb 02 '15 at 13:15
  • I've added a third option whereby a label is shown or collapsed based on whether the password box is empty. Obviously you could adjust the xaml to put the label wherever you wanted. – Gary Wright Feb 02 '15 at 15:05
  • Jesus dude, do some work yourself!! You will never learn anything if all you do is ask others to make iterations until you get what you want! – BenjaminPaul Feb 02 '15 at 15:07
  • Im new to wpf so just trying to get grips with it. – tjhack Feb 02 '15 at 16:15
  • Do any of those suggestions get you near to what you want to achieve? – Gary Wright Feb 03 '15 at 15:50
0

Check out IDataErrorInfo. More info here

pixelbadger
  • 1,556
  • 9
  • 24