【问题标题】:Binding from ViewModel to behaviour从 ViewModel 绑定到行为
【发布时间】:2015-07-06 05:50:47
【问题描述】:

我的 ViewModel 上有一个属性,我需要在我的行为中绑定到 BindableProperty,但我似乎无法绑定它。

private int _test;
    public int Test {
        get {
            return _test;
        }
        set {
            if (SetProperty (ref _test, value)) {
                _profileIsDirty = true;
                NotifyPropertyChanged ("AllowUpdate");

            }

        }
    }

这是行为中的属性

        public static readonly BindableProperty MinLengthProperty = BindableProperty.Create("MinLength", typeof(int), typeof(MinLengthValidator), 0);

    public int MinLength
    {
        get { return (int)GetValue(MinLengthProperty); }
        set { SetValue(MinLengthProperty, value); }
    }

这是 ViewModel 上的属性,这就是我尝试在 XAML 中绑定它的方式

<behave:TelephoneNumberValidatorBehaviour x:Name="phoneValidator" MinLength="{Binding Test, Mode=OneWay}"/>

但它永远不会绑定。我在这里做错了吗?

【问题讨论】:

    标签: c# xaml mvvm xamarin xamarin.forms


    【解决方案1】:

    您是否尝试将 NotifyPropertyChanged 的​​值更改为“Test”而不是“AllowUpdate”?否则,永远不会通知 UI Test 的值已更改。相反,它会提出 AllowUpdate 属性的值已更改,而您的 ViewModel 中不存在该值。

    【讨论】:

    • 是的,我有,SetProperty 方法中有一个内置通知,所以不幸的是它不是。
    【解决方案2】:

    BindableProperty.Create 的第三个参数应该是定义 BindableProperty 的类的类型。根据您的示例,我想应该是 typeof(TelephoneNumberValidatorBehaviour) 而不是 typeof(MinLengthValidator)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多