简体   繁体   中英

How to code A mandatory entry in this field

<Border Grid.Row="1" Background="Gray" Padding="7">
  <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,30,0">
    <optimumScheduler:LookUpOKButton Grid.Row="1" Content="OK" HorizontalAlignment="Center" Padding="20,0,20,0" Margin="0,0,20,0"/>
    <optimumScheduler:LookUpCancelButton Grid.Row="1" Content="Cancel" HorizontalAlignment="Center" Padding="20,0,20,0"/>

I need to condition the entry. I need to condition the OK button. If the user does not enter one of the following: Patient, Doctor/Therapist, DEpartment then we would want to disable the OK button or not allow entry.

here they are defined. one of them. How would i code that it has to have an entry in Subject

public static string FormatAppointmentFormCaption(bool allDay, string subject, bool readOnly)
{
    string format = allDay ? "Event - {0}" : "Appt Editor - {0}";
    string text = subject;
    if (string.IsNullOrEmpty(text))
         text = "Untitled";
    text = String.Format(CultureInfo.InvariantCulture, format, text);
    if (readOnly)
         text += " [Read only]";
    return text;
}

I recommend using the IDataErrorInfo interface and triggering the IsEnabled property of your button on the Validation.HasError property for your entry.

For example, your ViewModel may look like:

public class UserEntry: IDataErrorInfo
{
    public string UserType{get;set;}

    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            if(propertyName=="UserType")
            {
                // This is greatly simplified -- your validation may be different
                if(UserType != "Patient" || UserType != "Doctor" || UserType != "Department")
                {
                    return "Entry must be either Patient, Doctor, or Department.";
                }
            }
            return null;
        }
    }

    string IDataErrorInfo.Error
    {
        get
        {
            return null; // You can implement this if you like
        }
    }
}

And your View may have some XAML that looks similar to this:

<TextBox Name="_userType"
         Text="{Binding UserType, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}" />

<Button Command="{Binding OKCommand}"
        Name="OK">
  <Button.Style>
    <Style TargetType="Button">
      <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=_userType, Path=(Validation.HasError), Mode=OneWay}"
                      Value="False">
          <Setter Property="IsEnabled"
                  Value="True" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=_userType, Path=(Validation.HasError), Mode=OneWay}"
                      Value="True">
          <Setter Property="IsEnabled"
                  Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

Check out this link. It was very helpful for me at the time!

ICommand Implementation

If by any chances you cannot accomplish using ICommand , use an if statement and set the element IsEnabled property to false:

if(String.IsNullOrEmpty(Patient) || String.IsNullOrEmpty(Doctor) || String.IsNullOrEmpty(Therapist) || String.IsNullOrEmpty(DEpartment))
{
    yourElement.IsEnabled = false
}

Assuming that Patient , Doctor , Therapist and DEpartment is a string property (ie TextBox.Text, Label.Content, and so on...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM