【发布时间】:2017-02-15 00:03:02
【问题描述】:
我的应用中有一个表单,我正在使用行为进行客户端验证。
我想将一个值传递给最大验证器的行为。
值存储在我的 ViewModel 中,我试图将值传递给行为,但是它将零传递给行为而不是值。
我尝试使用硬编码值,效果很好。
我的问题是:
是否可以将 ViewModel 属性传递给行为?
XAML:
<Entry Text="{Binding EventSubscription.Attendees, Mode=TwoWay }" WidthRequest="50" x:Name="attendees" >
<Entry.Behaviors>
<behaviours:NullEntryValidationBehaviour x:Name="GroupValidator"/>
<behaviours:MaxAttendeesValidationBehaviour Max="{Binding EventSubscription.RemainingPlaces, Mode=TwoWay}"/>
</Entry.Behaviors>
</Entry>
C# 行为:
public class MaxAttendeesValidationBehaviour : Behavior<Entry>
{
public static BindableProperty IntProperty = BindableProperty.Create<MaxAttendeesValidationBehaviour, string>(tc => tc.MaxAttendeesAllowed, string.Empty, BindingMode.TwoWay);
public string MaxAttendeesAllowed
{
get
{
return (string)GetValue(IntProperty);
}
set
{
SetValue(IntProperty, value);
}
}
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
base.OnAttachedTo(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
bool IsValid = false;
if (!string.IsNullOrEmpty(e.NewTextValue))
{
IsValid = true;//Int32.Parse(e.NewTextValue) <= MaxAttendeesAllowed;
}
((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red;
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
}
【问题讨论】:
标签: c# mvvm xamarin.forms