首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在后面的代码中设置"{Binding}“?

如何在后面的代码中设置"{Binding}“?
EN

Stack Overflow用户
提问于 2018-01-24 11:26:17
回答 1查看 1.5K关注 0票数 1

有一个来自托马斯·莱韦斯克的好的控制

代码语言:javascript
运行
复制
<local:BindingProxy x:Key="proxy" Data="{Binding}" />

我想将Data初始化隐藏到构造函数中,只需像这样使用它:

代码语言:javascript
运行
复制
<local:BindingProxy x:Key="proxy" />

下面是我不工作的尝试(在链接模中完成代码):

代码语言:javascript
运行
复制
public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));

    public BindigProxy()
    {
        BindingOperations.SetBinding(this, DataProperty, new Binding { Source = this });
    }
}

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-24 11:35:19

不要设置绑定的Source属性。在XAML中也没有这样做。

与XAML表达式Data="{Binding}"等价的是

代码语言:javascript
运行
复制
BindingOperations.SetBinding(this, DataProperty, new Binding());
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48421358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档