如何编码此字段中的强制条目

时间:2013-10-03 19:55:35

标签: c# wpf

<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"/>

我需要调整条目。我需要调整OK按钮。如果用户没有输入以下某个:患者,医生/治疗师,DEpartment,那么我们要禁用“确定”按钮或不允许输入。

这里定义了它们。其中之一。我将如何编码它必须在主题

中有一个条目
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;
}

2 个答案:

答案 0 :(得分:1)

我建议您使用IDataErrorInfo界面并在IsEnabled媒体资源上触发按钮的Validation.HasError属性。

例如,您的ViewModel可能如下所示:

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
        }
    }
}

您的View可能有一些与此类似的XAML:

<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>

答案 1 :(得分:0)

查看此链接。那对我来说非常有帮助!

ICommand Implementation

如果您无法使用ICommand完成任务,请使用if语句并将元素IsEnabled属性设置为false:

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

假设PatientDoctorTherapistDEpartment是字符串属性(即TextBox.Text,Label.Content等等)。