【问题标题】:Set binding value as a parameter on a behavior from another control value in Xamarin Forms将绑定值设置为 Xamarin Forms 中另一个控件值的行为参数
【发布时间】:2026-02-04 10:55:01
【问题描述】:

我正在尝试使用条目行为验证条目值。为此,我需要将值从同一个 xaml 中的另一个控件传递给行为类。

我有以下两个控件

    <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}"
不工作。

【问题讨论】:

    标签: xamarin xamarin.forms behavior


    【解决方案1】:

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

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

    【讨论】:

    • 我试过了,但收到此错误没有为“LoadQty”找到属性、可绑定属性或事件,或者值和属性之间的类型不匹配
    • @Zeeshan,我运行项目时没有报错,你看到RegisterQtyBehavior类,继承Behavior,有此类中的 BindableProperty LoadQty
    • 是的,我遵循了与您在示例中给出的代码相同的代码,但我在运行时遇到类型异常,我已经编辑了我的代码,请检查。
    最近更新 更多