ContentView中的BindableProperty无法正常工作

Len*_*ijn 7 c# xaml xamarin xamarin.forms

我做了一个非常简单的Yes/No RadioBox控件.

在它的代码背后我有以下内容BindableProperty:

    public static readonly BindableProperty SelectedValueProperty = BindableProperty.Create(
        "SelectedValue",
        typeof(int),
        typeof(YesNoView),
        0,
        BindingMode.TwoWay);
Run Code Online (Sandbox Code Playgroud)

它的属性:

    public int SelectedValue
    {
        get { return (int) GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }
Run Code Online (Sandbox Code Playgroud)

此属性使用以下内容绑定在ContentView-XAML中:

          <DataTrigger Binding="{Binding SelectedValue}" Value="0" TargetType="Image">
            <Setter Property="Source" Value="radiobutton_checked.png" />
          </DataTrigger>
Run Code Online (Sandbox Code Playgroud)

每当我在某个页面中使用我的ContentView并将其设置SelectedValue为常量值时,一切正常!

但是,当我使用{Binding SomeValue}而不是常量值时,在SomeValue我使用ContentView的页面上的属性(带有通知),它将简单地拒绝做任何事情.每当我设置SomeValue一些值时,它永远不会到达ContentView,它让我疯狂为什么它不起作用.

即使我调整BindableProperty也指定了propertyChanged事件,除非我恢复到Xaml中的常量值而不是绑定,否则它永远不会被触发.

任何人都可以解释为什么会发生这种情况并且不像我期望的那样工作吗?

编辑:我应该在页面和视图中添加,BindingContext设置为此.

Len*_*ijn 29

经过4个小时的浪费,我想出来了......

永远不要BindingContext = this;在你的ContentView中,因为它从页面试图数据绑定到它劫持上下文.而是使用Content.BindingContext = this;

  • @Sven-MichaelStübe:使用ContentView的页面设置了它的BindingContext,然后将其用于所有子元素,包括自定义ContentView。如果再次在内容视图中设置上下文,则将其覆盖。 (2认同)
  • 伙计,你让我免于周末工作。万分感谢。 (2认同)