user1447343
user1447343

Reputation: 1467

Can't bind to View's property

In my view's xaml file, I have this line:

TextBox Text="{Binding MyModel.Text}"

Everytime I ran the program, it gave me this error message:

System.Windows.Data Error: 40 : BindingExpression path error: 'MyModel' property not found on 'object' ''MyModel' (HashCode=56593137)'. BindingExpression:Path=MyModel.Text; DataItem='MyModel' (HashCode=56593137); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

I'm sure my spelling is right.

I set my view's DataContext to ViewModel. Could that be a problem?

Upvotes: 1

Views: 86

Answers (3)

Big Daddy
Big Daddy

Reputation: 5234

Since you're view is bound to your view-model (good), then your view-model needs to have a property that your view will bind to:

TextBox Text="{Binding MyViewModelsProperty}"

In your situation, you'll need to set your model's property from your view-model (MyViewModelsProperty setter).

Let me know if you need more info.

Upvotes: 0

Fede
Fede

Reputation: 44068

Just TextBox Text="{Binding Text}"

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26078

If your DataContext is set to MyModel you should just have to write:

<TextBox Text="{Binding Text}"/>

Adding the extra MyModel is repetitive and results in looking for MyModel.MyModel.Text.

Upvotes: 2

Related Questions