1

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.

1 Answer 1

1

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.

5
  • Ok, so apparently I'm going to need to change up my question. My ViewModel is actually in C++/CLI, so this method doesn't port over intuitively. I'm really sorry for switching things up, I figured things would port nicer, and C# was easier to write/read.
    – user6732861
    Dec 28, 2016 at 0:59
  • @SanjayCruze I'd close/accept this, and make a new question that shows the actual code (or at least code that's in C++/CLI) Dec 28, 2016 at 1:00
  • Um, just changed it. Should I change it back, accept this answer, and ask a new question?
    – user6732861
    Dec 28, 2016 at 1:02
  • Thank you. Now how do I expose the get function? (And do I need a separate MyThingClass, one to be a property, one to be the variable?
    – user6732861
    Dec 28, 2016 at 1:10
  • Ended up making a local variable of MyThingClass, and adding a full get function onto MyThing. Is there a shorter way (since IsEnabled is a property)? And thank you very much, and I'm really sorry about the confusion.
    – user6732861
    Dec 28, 2016 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.