[英]WPF Binding error to class property, when other properties work fine
我知道还有很多类似的其他SO帖子,但是似乎没有一个我绑定到class属性的场景,而其他属性也很好。 我正在使用MVVM模式, 其中我的视图模型位于c ++ / cli中 。 由于其他属性有效,因此它不是我的View的DataContext
。 我在绑定到ViewModel中的bool的Button
上有一个IsEnabled
。 现在,当尝试绑定到类似的东西时,
private:
bool thing = false;
public:
bool Thing
{
bool get() { return thing; }
void set(bool value) { thing = value; }
}
它工作正常。 但是后来我做了类似的事情,
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}"/>
某件事决定中断,该属性未绑定,并且在输出中我得到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')
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')
如果我遗漏了任何东西,或者没有任何意义,请提出问题。
当你写:
MyThingClass MyThing;
您创建一个名为MyThing
的公共字段 ,而不是一个属性 。
为了使用数据绑定,需要将其定义为属性而不是字段:
property MyThingClass^ MyThing;
此外,如果您希望属性更新UI,则MyThingClass
将需要实现INotifyPropertyChanged
。 还要注意,因为它是一个托管类,所以需要使用句柄类型( ^
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.