2017-11-20 12 views
0

タイトルの意味は次のとおりです。入力が範囲(この場合は0〜10)にあるかどうかをチェックし、エントリの背景色を赤くする動作(Validatorと呼ばれます)にバインドされた "IsValid"(bool)というプロパティを持つエントリがありますまたは透明である。それはうまく動作します。しかし、ViewModelに同じロジックがあり(入力が範囲内にあるかどうかを確認するため)、ダイアログメッセージが表示されない場合は、バリデータのIsValidに直接バインドして、viewModelでbinddを使用したい(IsBinReferenceValid )abdはvmからlocigを削除します。現在、私のVM IsBinReferenceValidのプロパティは、バインディングが機能しないことを示す方法で変更されていません。問題はどこから来ていることがある場合つまり、(、同時にプロパティを参照してバインドする方法は?

<userControl:DetailedEntry 
      PlaceholderLabel="{x:Static locale:BinPrintLang.BinRef}" 
      Text="{Binding BinTextEntry}" 
      TextColor="{StaticResource PrimaryColor}" 
      BgColor="White" 
      BorderColor="{StaticResource DisableColor}" 
      VerticalOptions="CenterAndExpand" 
      IsLabelVisible="True" 
      Label="Bin Reference" 
      IsImportant="True" 
      IsValid="{Binding Source={x:Reference InputLengthValidator}, Path=IsValid}"> 
      <userControl:DetailedEntry.EntryBehavior> 
       <ui:InputLengthValidator x:Name="InputLengthValidator" 
             MinValue="0" 
             MaxValue="10" 
             IsValid="{Binding Source=IsBinReferenceValid, Mode=OneWayToSource}"/> 
      </userControl:DetailedEntry.EntryBehavior> 
     </userControl:DetailedEntry> 

私は同時に参照すると、プロパティにバインドすることができますどのように任意のアイデアことも可能である。ここでは

は、XAMLコードであります)?

ベースのバリコード:

public class ValueInRangeValidator : Validator<Entry> 
{ 
    private static BindableProperty MinValueProperty = 
     BindableProperty.Create("MinValue", typeof(decimal?), typeof(ValueInRangeValidator)); 

    public decimal? MinValue 
    { 
     get { return (decimal?) GetValue(MinValueProperty); } 
     set 
     { 
      SetValue(MinValueProperty, value); 
      OnPropertyChanged(); 
     } 
    } 

    public static BindableProperty MaxValueProperty = 
     BindableProperty.Create("MaxValue", typeof(decimal?), typeof(ValueInRangeValidator)); 

    public decimal? MaxValue 
    { 
     get { return (decimal?) GetValue(MaxValueProperty); } 
     set 
     { 
      SetValue(MaxValueProperty, value); 
      OnPropertyChanged(); 
     } 
    } 


    public virtual void Bindable_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     decimal i = 0; 
     IsValid = decimal.TryParse(e.NewTextValue, out i); 

     IsValid = IsValid && (MinValue == null ? i >= decimal.MinValue : i >= MinValue); 
     IsValid = IsValid && (MaxValue == null ? i <= decimal.MaxValue : i <= MaxValue); 
    } 

    protected override void OnAttachedTo(Entry bindable) 
    { 
     bindable.TextChanged += Bindable_TextChanged; 
    } 

    protected override void OnDetachingFrom(Entry bindable) 
    { 
     bindable.TextChanged -= Bindable_TextChanged; 
    } 
} 

InputLengthValidatorコード:

public class InputLengthValidator : ValueInRangeValidator 
{ 

    public override void Bindable_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     var max = (int) MaxValue; 
     var min = (int) MinValue; 
     var textLenght = e.NewTextValue.Length; 
     IsValid = textLenght >= min && textLenght < max; 
    } 
} 
+1

問題のあなたの行動のロジックを追加します。 –

+0

@ZiyadGodilコードを追加しました。ありがとうございます –

+0

unfocusに表示ダイアログ(有効でない場合)したいですか? –

答えて

0

私は(カスタム)別に加入することにより、検証作業を取得するためにをIsWarningと呼ばれる私のDetailedEntryコントロールのバインド可能なプロパティを管理していました。

<userControl:DetailedEntry 
rid.Row="1" 
Grid.Column="0" 
Label="{x:Static locale:GoodsReceiptLang.NumLabels}" 
Text="{Binding NumberOfLabels, Mode=TwoWay}" 
TextColor="{StaticResource PrimaryColor}" 
Keyboard="Numeric" 
IsImportant="True" 
IsWarning="{Binding ShowWarning}"> 
</userControl:DetailedEntry> 

マイVM:

private bool CanPrint() 
    { 
     var errors = new List<string>(); 

     ShowWarning = false; 

     if (SelectedPrinter == null) 
      errors.Add(CommonLang.SelectPrinterErrorMsg); 

     if (string.IsNullOrEmpty(NumberOfLabels) || !int.TryParse(NumberOfLabels, out int numLabels)) 
     { 
      ShowWarning = true; 
      errors.Add(CommonLang.NotValidInput); 
     } 

     if (errors.Any()) 
     { 
      ShowErrorMessage(string.Join(" ", errors.ToArray())); 
      return false; 
     } 

     return true; 
    } 
関連する問題