如何正确绑定?
我有一个 MyClass 类,它实现了 INotifyPropertyChanged,并且它有一些必须在某个页面中绑定的属性。在我的页面中
private MyClass myclass;
,我在页面构造函数中编写了
ContentPanel.DataContext = myclass;
当我将 myclass 分配给某个 MyClass 对象(我从某个回调中获得)时,页面中没有显示任何内容。
但是,当我编写必须更改的属性而不是 page.cs 中的 MyClass 类并绑定它们时,它可以正常工作。 或者当我给出
ContentPanel.DataContext = this;
并在 xaml 中编写
{binding this.myclass.property}
时,它也可以正常工作。
这是回调
public void GetCommonInfoCallback(UserCommonInfo userCommonInfo)
{
CommonInfo = userCommonInfo;
}
,其中 UserCommonInfo 是 MyClass,CommonInfo 是 myclass。
private UserCommonInfo userCommonInfo ;
public UserCommonInfo CommonInfo
{
get
{
return userCommonInfo;
}
set
{
if (userCommonInfo != value)
{
userCommonInfo = value;
OnPropertyChanged("CommonInfo");
}
}
}
我不明白我的错误在哪里。你能帮助我吗?
I have a class MyClass, which implements INotifyPropertyChanged, and it has some properties that must be bound in some page. In my page I have
private MyClass myclass;
and in the page constructor I write
ContentPanel.DataContext = myclass;
When I assign myclass to some MyClass object, which I get from some callback, nothing is shown in page.
But when I write the properties that I must change instead of MyClass class in page.cs and bind them it work correctly.
Or when I give
ContentPanel.DataContext = this;
and in xaml I write
{binding this.myclass.property}
it also works correctly.
Here is callback
public void GetCommonInfoCallback(UserCommonInfo userCommonInfo)
{
CommonInfo = userCommonInfo;
}
where UserCommonInfo is MyClass, and CommonInfo is myclass.
private UserCommonInfo userCommonInfo ;
public UserCommonInfo CommonInfo
{
get
{
return userCommonInfo;
}
set
{
if (userCommonInfo != value)
{
userCommonInfo = value;
OnPropertyChanged("CommonInfo");
}
}
}
I can't understand where is my mistake. Can you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您设置 DataContext 时,它是用于数据绑定的 MyClass 的特定实例。因此,执行后
您可以稍后执行
,并且数据将在绑定控件中更新(假设这不是 OneTime 绑定,而是 OneWay 或 TwoWay 绑定)。
如果我正确理解你的问题,你想更改绑定以使用 MyClass 的不同实例。
此时,控件仍然绑定到 MyClass 的先前实例。您可以通过更新 DataContext 来更改这一点:
您编写的第二种方法
代表了不同的样式,其中您使页面类也充当数据绑定的数据模型实例。
在这种情况下,您不会更改数据绑定以使用数据模型的新实例(页面实例“this”不会更改)。恕我直言,将页面和数据模型分开是非常有价值的,所以我宁愿不使用 DataContext = 这种方法。
When you set DataContext, it is the specific instance of MyClass that is used for data binding. So after executing
you could later execute
and the data will be updated in the bound control (assuming this is not a OneTime binding, but OneWay or TwoWay binding instead).
If I understand you question correctly, you want to change the binding to use a different instance of MyClass.
At this point, the controls are still bound to the previous instance of MyClass. You can change that by updating the DataContext:
The second approach that you wrote, with
represents a different style, where you are making the page class also serve as the data model instance for data binding.
In this case, you are not changing the data binding to use a new instance of the data model (the page instance, 'this', is not changing). IMHO, there is very valuable to separate the page and the data model, so I prefer to not use the DataContext = this approach.