绑定导致 Element 和 UserControl 之间的错误

Binding causing error between Element and UserControl

我的页面中有一个用户控件,它有一个布尔值 属性 我正在尝试将其绑定到切换按钮。但是,我收到一个我不明白的错误。

按钮:

<AppBarToggleButton x:Name="btnFoo"
                    Icon="Edit"
                    Checked="btnFoo_Checked"
                    Unchecked="btnFoo_Checked"/>

该页面中的用户控件:

<local:ucMyControl FooBool="{Binding ElementName=btnFoo, Path=IsChecked}" />

用户控件的 public 属性:

public bool FooBool { get; set; }

初始化该控件时出现此错误,

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in PhoneApp.exe but was not handled in user code

WinRT information: Failed to assign to property '%0'. [Line: 121 Position: 42]

Additional information: The text associated with this error code could not be found

为什么属性不能设置?我必须使用值转换器吗?

为了将值绑定到 属性,它必须是 DependencyProperty

 public bool FooBool
    {
        get { return (bool)GetValue(FooBoolProperty); }
        set { SetValue(FooBoolProperty, value); }
    }

    public static readonly DependencyProperty FooBoolProperty =
        DependencyProperty.Register("FooBool", typeof(bool), typeof(ucMyControl), new PropertyMetadata(false));