将绑定值设置为 Xamarin Forms 中另一个控件值的行为参数

Set binding value as a parameter on a behavior from another control value in Xamarin Forms

我正在尝试使用输入行为来验证输入值。为此,我需要将值从同一 xaml.

中的另一个控件传递给行为 class

我有以下两个控件

    <Entry
    x:Name="RegisterQty"
    Grid.Column="0"
    WidthRequest="120"
    TextChanged="RegisterQty_TextChanged"
    TextColor="Black"
    HorizontalOptions="Start"
    VerticalOptions="Center"
    FontAttributes="Bold"
    PlaceholderColor="Black" 
    Keyboard="Numeric"
    FontSize="20">
    <Entry.Behaviors>
           <local:RegisterQtyBehavior LoadQty = "{Binding BindingContext.qty, Source={x:Reference RegisterQty}} "/>
    </Entry.Behaviors>

    </Entry>

public class RegisterQtyBehavior : Behavior<Entry>
{        
    public static readonly BindableProperty LoadQtyProperty = BindableProperty.Create(nameof(LoadQty), typeof(double), typeof(RegisterQtyBehavior), 0);

    public double LoadQty
    {
        get { return (double)GetValue(LoadQtyProperty); }
        set { SetValue(LoadQtyProperty, value); }
    }
    protected override void OnAttachedTo(Entry entry)
    {
        entry.TextChanged += OnEntryTextChanged;
        base.OnAttachedTo(entry);
    }

    protected override void OnDetachingFrom(Entry entry)
    {
        entry.TextChanged -= OnEntryTextChanged;
        base.OnDetachingFrom(entry);
    }

    void OnEntryTextChanged(object sender, TextChangedEventArgs args)
    {
        double result = LoadQty;

        bool isValid = double.TryParse(args.NewTextValue, out result);
        ((Entry)sender).TextColor = isValid ? Color.Red : Color.Default;
    }
}

我想将标签绑定数量传递给入口控制。我怎么能做到这一点?直接添加LoadQty = "{Binding Qty}"
不起作用。

根据你的描述,你想在入口行为中使用绑定参数,对吗?如果是,我做了一个样品你可以看看:

   <Entry x:Name="registerqty">
            <Entry.Behaviors>
                <local:RegisterQtyBehavior LoadQty="{Binding BindingContext.qty, Source={x:Reference registerqty}}}" />
            </Entry.Behaviors>

        </Entry>

public partial class Page37 : ContentPage, INotifyPropertyChanged
{
    private int _qty;
    public int qty
    {
        get { return _qty; }
        set
        {
            _qty = value;
            RaisePropertyChanged("qty");
        }
    }

    public Page37 ()
    {
        InitializeComponent ();
        qty = 100;

        this.BindingContext = this;
    }


    public event PropertyChangedEventHandler PropertyChanged;       
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class RegisterQtyBehavior:Behavior<Entry>
{
    public static readonly BindableProperty LoadQtyProperty = BindableProperty.Create("LoadQty", typeof(int), typeof(RegisterQtyBehavior), defaultValue: 0);

    public int LoadQty
    {
        get { return (int)GetValue(LoadQtyProperty); }
        set
        {
            SetValue(LoadQtyProperty,value);
        }
    }

    protected override void OnAttachedTo(Entry entry)
    {
        entry.TextChanged += OnEntryTextChanged;
        base.OnAttachedTo(entry);
    }

    protected override void OnDetachingFrom(Entry entry)
    {
        entry.TextChanged -= OnEntryTextChanged;
        base.OnDetachingFrom(entry);
    }

    void OnEntryTextChanged(object sender, TextChangedEventArgs args)
    {
        //double result;
        //bool isValid = double.TryParse(args.NewTextValue, out result);
        //((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
        if(!string.IsNullOrEmpty(args.NewTextValue))
        {
            if (Convert.ToInt32(args.NewTextValue) > LoadQty)
            {
                ((Entry)sender).TextColor = Color.Red;
            }
            else
            {
                ((Entry)sender).TextColor = Color.Default;
            }
        }

    }
}