ContentView 中的 BindableProperty 不起作用 [英] BindableProperty in ContentView not working

查看:16
本文介绍了ContentView 中的 BindableProperty 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a very simple Yes/No RadioBox control.

在它后面的代码中,我有以下 BindableProperty:

In it's code behind I have the following BindableProperty:

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

它的属性:

    public int SelectedValue
    {
        get { return (int) GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }

此属性使用如下方式绑定在 ContentView-XAML 中:

This property is bound in the ContentView-XAML using something like this:

          <DataTrigger Binding="{Binding SelectedValue}" Value="0" TargetType="Image">
            <Setter Property="Source" Value="radiobutton_checked.png" />
          </DataTrigger>

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

Whenever I use my ContentView in some Page and set the SelectedValue to a constant value, everything works fine!

但是,当我使用 {Binding SomeValue} 而不是常量值时,其中 SomeValue 是我使用的页面上的属性(带通知)ContentView 上,它会干脆拒绝做任何事情.每当我将 SomeValue 设置为某个值时,它永远不会到达 ContentView,这让我发疯,为什么它不工作.

However, when I use a {Binding SomeValue} instead of a constant value, where SomeValue is a property (with notify) on the Page I'm using the ContentView on, it will simply refuse to do anything. Whenever I set SomeValue to some value, it never reaches the ContentView and it's driving me insane why it isn't working.

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

Even when I adjust the BindableProperty to also specify a propertyChanged event, it is simply never triggered unless I revert back to a constant value in the Xaml, rather than a binding.

有人可以解释为什么会发生这种情况并且没有按我预期的那样工作吗?

Could anyone shed a light as to why this is happening and not working as I would expect?

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

I should add that in both the Page and the View the BindingContext is set to this.

推荐答案

我总共浪费了 4 个小时才弄明白...

I figured it out after a total of 4 hours wasted...

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

Never do BindingContext = this; in your ContentView, as it hijacks the context from the Page trying to databind to it. Instead use Content.BindingContext = this;

这篇关于ContentView 中的 BindableProperty 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆