绑定到实例内的属性
我正在开发一个从后端获取数据的项目,即数据不断动态变化,前端不必关心。 我需要将此数据公开给 wpf,以便有人可以通过表达式混合将内容绑定到 wpf 中的数据。只读就可以了。 简而言之,如果我的控制流程大致如下,我如何对类型为“Foo”的类“foo”的实例中的属性 Foostring 进行数据绑定:
public partial class Window1 : window
{
public Window1()
{
InitializeComponent();
Foo foo = new foo;
}
// my text box is defined in the xaml of this window.
}
public ref class Foo
{
Foo()
{
FooProperty = "work,dammit";
}
private string _foostring="";
public string FooProperty
{
get {return _foostring;}
set {foostring=value;}
}
}
如果在 Foo 类的构造函数中,我可以让事情正常工作我在文本框上设置绑定,如果我继承 INotifyPropertyChanged 并在 FooProperty 的设置上引发一个事件。 但是,这不会将此变量暴露给表达式混合——它并不是真正设置数据源。我尝试在 xaml 中设置绑定,它可以编译但不会更新。 任何帮助将不胜感激!
I'm working on a project that is getting data from a backend, ie, the data is constantly changing dynamically, and the frontend shouldn't have to care.
I need to expose this data to wpf such that someone can bind things to the data in wpf via expression blend. Readonly is fine.
In short, how do I do databinding to the property Foostring in a instance of a class "foo" of type "Foo" if my flow of control is roughly the following:
public partial class Window1 : window
{
public Window1()
{
InitializeComponent();
Foo foo = new foo;
}
// my text box is defined in the xaml of this window.
}
public ref class Foo
{
Foo()
{
FooProperty = "work,dammit";
}
private string _foostring="";
public string FooProperty
{
get {return _foostring;}
set {foostring=value;}
}
}
I can get things to work if in the constructor of the Foo class I set binding on the text box, and if I inherit from INotifyPropertyChanged and raise an event on the setting of FooProperty.
However, this does not expose this variable to expression blend--it's not really setting a datasource. I've tried to set the binding in xaml and it compiles but doesn't update.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[Rokujolady回答]
为了后代,我发布了解决方案:
当我应该从 xaml 实例化时,我正在从 c# 实例化对象 foo,因为 xaml 正在创建它自己的实例,该实例没有被更新,因为我正在更新 c# 实例化的实例。显然,您无法从 xaml 引用在 c# 中创建的对象,但可以从 c# 引用在 xaml 中创建的对象。代码最终是这样的:
XAML:
C# 代码如下所示:
[Answer by Rokujolady]
For posterity, I've posted the solution:
I was instancing the object foo from c# when I should have been instancing from xaml, because xaml was creating its own instance which was not being updated because I was updating the c# instantiated instance. Apparently you can't reference an object created in c# from xaml, but you can reference one created in xaml from c#. The code ended up like this:
XAML:
C# Code looked like this: