Reputation:
I know there are a bunch of other similar SO posts like this, but none seem to have my scenario where I'm binding to a class property, AND other properties work just fine. I'm using MVVM pattern, where my viewmodel is in c++/cli. Since other properties work, it isn't my DataContext
of my View. I have a IsEnabled
on a Button
binding to a bool in my ViewModel. Now, when try to bind to something like,
private:
bool thing = false;
public:
bool Thing
{
bool get() { return thing; }
void set(bool value) { thing = value; }
}
It works just fine. But then I do something like,
public ref class MyThingClass
{
private:
bool isEnabled = false;
public:
bool IsEnabled
{
bool get() { return thing; }
void set(bool value) { thing = value; }
};
};
public:
//Not sure if I need a handle (^) on this or not
MyThingClass MyThing;
//XAML
<Button x:Name="MyButton" IsEnabled="{Binding MyThing.IsEnabled}"/>
Something decides to break, the property doesn't bind, and in the output I get System.Windows.Data Error: 40 : BindingExpression path error: 'MyThing ' property not found on 'object' ''MyViewModel' (HashCode=66641179)'. BindingExpression:Path=MyThing.IsEnabled; DataItem='MyViewModel' (HashCode=66641179); target element is 'Button' (Name='MyButton'); target property is 'IsEnabled' (type 'Boolean')
Please ask questions if I left anything out, or something doesn't make sense.
Upvotes: 1
Views: 177
Reputation: 564383
When you write:
MyThingClass MyThing;
You create a public field named MyThing
, not a property.
This needs to be defined as a property, not a field, in order to work with data binding:
property MyThingClass^ MyThing;
Also, if you want the property to update the UI, your MyThingClass
will need to implement INotifyPropertyChanged
. Also note the need for the handle type (^
) since it's a managed class.
Upvotes: 1