绑定到实例内的属性

发布于 2024-10-18 16:25:13 字数 827 浏览 0 评论 0原文

我正在开发一个从后端获取数据的项目,即数据不断动态变化,前端不必关心。 我需要将此数据公开给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2024-10-25 16:25:13

[Rokujolady回答]
为了后代,我发布了解决方案:
当我应该从 xaml 实例化时,我正在从 c# 实例化对象 foo,因为 xaml 正在创建它自己的实例,该实例没有被更新,因为我正在更新 c# 实例化的实例。显然,您无法从 xaml 引用在 c# 中创建的对象,但可以从 c# 引用在 xaml 中创建的对象。代码最终是这样的:
XAML:

<Window.Resources>
<local:Foo x:Name "foo" x:Key="FooDataSource" d:IsDataSource="True"/>
...
</Window.Resources>
<Grid x:name="blah">
<Grid DataContext="{Binding Source={StaticResource FooDataSource}}">
<TextBlock x:Name="myText" Text="{Binding FooProperty, Mode=Default}"></TextBlock></Grid>

C# 代码如下所示:

public partial class Window1:window
{
     Foo myFooVariable = null;
     public Window1()
     {
          InitializeComponent();
          myFooVariable = this.Resources["FooDataSource"] as Foo;
          myFooVariable.FooString = "Work, Dammit";
     }
}

[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:

<Window.Resources>
<local:Foo x:Name "foo" x:Key="FooDataSource" d:IsDataSource="True"/>
...
</Window.Resources>
<Grid x:name="blah">
<Grid DataContext="{Binding Source={StaticResource FooDataSource}}">
<TextBlock x:Name="myText" Text="{Binding FooProperty, Mode=Default}"></TextBlock></Grid>

C# Code looked like this:

public partial class Window1:window
{
     Foo myFooVariable = null;
     public Window1()
     {
          InitializeComponent();
          myFooVariable = this.Resources["FooDataSource"] as Foo;
          myFooVariable.FooString = "Work, Dammit";
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文