由于ContentProperty无法识别,因此代码隐藏中的WPF标签上的绑定设置不起作用

时间:2016-01-07 09:28:41

标签: wpf data-binding

我的视图中有以下方法;

Label GetSummaryLabel(Panel panel, ServiceChargeType type)
{
    var chargeTextbox = panel.Children[1];

    var summarybinding = new Binding { Source = chargeTextbox, Path = new PropertyPath("Text") };
    var summaryLabel = new Label();
    summaryLabel.SetBinding(ContentProperty, summarybinding);

    return summaryLabel;
}

这很好用,但我想把这个函数移到一个帮助器类,当我这样做时,我得到以下编译错误;

“无法解析符号'ContentProperty'

为什么?

1 个答案:

答案 0 :(得分:1)

ContentProperty是ContentControl类的static成员。当您在视图中时(例如UserControlWindow),您已经位于ContentControl的某个派生类中,且ContentProperty可用。但是,当您将代码移动到帮助程序时,您需要使用它的类名对ContentProperty进行分类。

HTH