【问题标题】:Acces to WPF value though proxy in code通过代码中的代理访问 WPF 值
【发布时间】:2017-03-06 13:16:54
【问题描述】:

我是 WPF 的初学者。

它是关于数据绑定,在使用 Helix ToolKit 定义的 3D 对象上。 关键是使用的对象不是从FrameworkElement派生的,所以我在运行时收到错误:

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。

我依靠this blog 来解决我的问题。

根据这篇文章,我的问题是如何从代码中设置数据的值?我是否必须实例化代理,或者我错过了什么棘手的事情?

非常感谢任何帮助!

【问题讨论】:

  • 显示您的具体问题以及您已经尝试过的内容
  • dataGrid.Columns[0].Binding = new Binding(whatever) ?

标签: c# wpf data-binding proxy


【解决方案1】:

好吧,这个问题被否决了,我想它不是很清楚......

其实我弄错了,我不需要在代码中访问代理实例。

在 ViewModel 中我声明数据变量 DataViewModel,在 XAML 中我使用 ViewModel 值设置代理变量 DataProxy,方法是:

<Proxy x:Key="proxyKey" DataProxy="{Binding DataViewModel}" />

要将值 Data3D 设置为 3D 对象,我按照博客中的操作:

<Object3D Data3D="{Binding DataProxy, Source={StaticResource ResourceKey=proxyKey}}"

视图的 Header 声明了 DataContextreferencing ViewModel。在 ViewModel 的代码中,我可以访问属性DataViewModel 来定义它的值。属性DataViewModel 在 ViewModel 中定义,以便可以绑定到 View。

它工作正常,没有更多错误......

【讨论】:

    猜你喜欢
    • 2011-01-08
    • 1970-01-01
    • 2010-11-21
    • 2020-01-05
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2011-07-18
    相关资源
    最近更新 更多