2013-10-03 24 views
0
<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按鈕。如果用戶未輸入以下內容之一:患者,醫生/治療師,部門,則我們希望禁用「確定」按鈕或不允許輸入。如何編碼此字段中的強制條目

這裏他們被定義。其中之一。我如何編碼,它必須有一個條目主題

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

只需綁定'Button'到'ICommand'並控制它是否使用'CanExecute'方法啓用。 –

+0

我明白了。你能指出我的第一步嗎? –

+0

我覺得@Bob。已經做了。 – JDB

回答

1

我建議使用IDataErrorInfo接口並觸發Validation.HasError屬性上按鈕的IsEnabled屬性以供您輸入。

例如,您的視圖模型可能看起來像:

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

而且你的觀點可能有一定的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> 
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等...)。