0

I'm creating a program in C# using WPF that will generate a PDF of a sale sign for a retail store (just practice). Since it is a sale sign, I need the user to enter a date to represent the end of the sale. I'm using the datepicker control to do this. I would like to use some binding validation to check two things:

  • A date is entered
  • That date is either today or some point in the future

I'm somewhat new to C# and WPF, so I'll provide an example of the type of validation I'm using in case there are other ways to do it:

I also have text boxes that have functioning binding. Here is the xaxml for one of those text boxes:

<TextBox Name="title" Style="{StaticResource errorStyle}" Height="24" HorizontalAlignment="Stretch" Margin="6,8,6,0" VerticalAlignment="Top" Grid.Column="1">
     <Binding Source="{StaticResource signData}" Path="SignTitle" UpdateSourceTrigger="Explicit">
          <Binding.ValidationRules>
               <ExceptionValidationRule/>
          </Binding.ValidationRules>
     </Binding>
</TextBox>

And the corresponding class that models the data:

class SignOrder
{
     ...
     public string SignTitle
     {
          get { return this.signTitle; }
          set
          {
               if ( (String.IsNullOrWhiteSpace(value)) || (value.Length >= 20) )
               {
                    throw new ApplicationException("Specify a title of less than 20 characters");
               }
               else
               {
                    this.signTitle = value;
               }
          }
     }
     ...
}

Do I need to create a converter class to work with the date? What would be the best way to validate the datepicker? What datatype is the value of the datepicker (string, int, date, ...)?

Thanks

3
Contributors
3
Replies
9
Views
6 Years
Discussion Span
Last Post by __avd
0

hummm
for valdation of datetimepicker write code on the dtppicker of change event
for eg. it sholud be today or greater then today

dtp.value <= Now.Date \\\dtp is name of control

0

You need to implements System.ComponentModel.IDataErrorInfo interface.

namespace wpfdatetimevalidation
{
    public class DateValidation : System.ComponentModel.IDataErrorInfo
    {

        public string Date { get; set; }

        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string result = null;

                if (columnName == "Date")
                {
                    DateTime dt;
                    DateTime.TryParse(Date, out dt);
                    if (dt == DateTime.MinValue || dt < DateTime.Now)
                        result = "Not a valid date";
                }
                return result;
            }
        }

        #endregion
    }
}

xaml markup

<Window x:Class="wpfdatetimevalidation.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300"
         
        xmlns:src="clr-namespace:wpfdatetimevalidation"
        >
    <Window.Resources>
        <src:DateValidation x:Key="data"/>
        <Style x:Key="DateInputError" TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
     
    
    <StackPanel Margin="20">
        <TextBlock>Enter Date </TextBlock>
        <TextBox Style="{StaticResource DateInputError}">
            <TextBox.Text>
              
                <Binding Path="Date" Source="{StaticResource data}"
                         ValidatesOnDataErrors="True"   
                         UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                         <ExceptionValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </StackPanel>
</Window>
0

You need to implements System.ComponentModel.IDataErrorInfo interface.

namespace wpfdatetimevalidation
{
    public class DateValidation : System.ComponentModel.IDataErrorInfo
    {

        public string Date { get; set; }

        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string result = null;

                if (columnName == "Date")
                {
                    DateTime dt;
                    DateTime.TryParse(Date, out dt);
                    if (dt == DateTime.MinValue || dt < DateTime.Now)
                        result = "Not a valid date";
                }
                return result;
            }
        }

        #endregion
    }
}

xaml markup

<Window x:Class="wpfdatetimevalidation.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300"
         
        xmlns:src="clr-namespace:wpfdatetimevalidation"
        >
    <Window.Resources>
        <src:DateValidation x:Key="data"/>
        <Style x:Key="DateInputError" TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
     
    
    <StackPanel Margin="20">
        <TextBlock>Enter Date </TextBlock>
        <TextBox Style="{StaticResource DateInputError}">
            <TextBox.Text>
              
                <Binding Path="Date" Source="{StaticResource data}"
                         ValidatesOnDataErrors="True"   
                         UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                         <ExceptionValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </StackPanel>
</Window>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.