Wizard
Wizard

Reputation: 1152

Binding causing error between Element and UserControl

I have a User Control in my page which has a boolean property I am trying to bind this to a Toggle button. However, I am getting an error that I don't understand.

The button:

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

The User Control in that page:

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

The User Control's public property:

public bool FooBool { get; set; }

I am getting this error when that control is initialised,

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

Why can the property not be set? Do I have to use a value converter?

Upvotes: 0

Views: 80

Answers (1)

Bells
Bells

Reputation: 1493

In order to bind a value to the property it must be a 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));

Upvotes: 1

Related Questions