Search code examples
windows-phone-8dependency-propertiesdata-binding

Binding custom dependency property gets databinding to string cannot convert exception


I need to set the Xaml property of a RichTextBox user control via a binding expression in Windows Phone 8, and I found that it is not a DP, so I have decided to inherit from a RichTextBox and add a DP that will change the Xaml property with PropertyChanged event, anyways the code looks like this, stripped out irrelevant parts.

public class RichTextBoxWithBindableXaml : RichTextBox
{
    public string BindableXaml
    {
        get { return (string)GetValue(BindableXamlProperty); }
        set { SetValue(BindableXamlProperty, value); }
    }
    public static readonly DependencyProperty BindableXamlProperty =
        DependencyProperty.Register("BindableXaml", 
                                    typeof(string), 
                                    typeof(RichTextBoxWithBindableXaml),
                                    new PropertyMetadata(0));    
}

 //xaml code

<local:RichTextBoxWithBindableXaml BindableXaml="{Binding PageContent , Mode=OneWay}"> </local:RichTextBoxWithBindableXaml>

And I get the following dreaded exception message: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'.

I have checked many solutions to these exceptions and similar problems with data binding, and still going through the suggested similar questions on the right, and still cannot see why a simple thing wont work for me. The code I listed above is just the simplest implementations of a DP with a binding expression. Btw, the source PageContent is from a INotifyPropertyChanged object, and it works, I know because, it can bind to TextBlock's Text property.

Am I missing out something so obvious? I wouldn't want to post question for such a straightforward thing, but I cant seem to solve in any way.

EDIT: Following P.S note turned out to be completely irrelevant.

P.S. My final doubt was on the way xmlns namespace local is loaded. It is loaded as clr assembly, could xaml parser think my custom inherited class as clr-only and confuse since clr properties are not dependency properties. Hope it doesnt sound stupid, i'm desperate. It is as such :

xmlns:local="clr-namespace:RumCli"

Solution

  • For each dependency property one must supply a non subscribed value (not a C# term here) which suites the type of object which the consumer will access.

    To quote MSDN Dependency Property Metadata

    Dependency property metadata exists as an object that can be queried to examine the characteristics of a dependency property.

    So for the value type results, a default for the different value types, such as a double is to use double.NaN. A decimal use decimal.Zero. While a string, string.empty is good as a base.

    That allows whatever operation which may blindly reflect off of the property, it can determine what its true property type is and access it accordingly.

    So assigning 0 to a string makes no sense in identifying that the property is a string which 0 identifies it as an integer. So the int as string is setting up a future runtime failures when objects try to assign bindings, styles and other items to it.