【问题标题】:DependencyProperty Binding to a programmatically created ListBoxDependencyProperty 绑定到以编程方式创建的 ListBox
【发布时间】:2012-03-05 17:42:29
【问题描述】:

我是 WPF / Silverlight 的新手,所以我很难描述我尝试做的事情。也许这就是我在 Stackoverflow 和 Google 上找不到答案的原因。

我尝试以编程方式绑定到DependyProperty

public static DependencyProperty MyDependencyProperty
        = DependencyProperty.RegisterAttached(
                            "...", 
                            typeof(...),
                            typeof(...),
                            new PropertyMetadata(...)
                      );
xmlns:MyXMLNS="clr-namespace:...."

<ListBox MyXMLNS:MyClass.MyDependencyProperty="...">
    // ....
</ListBox>

这已经按预期工作了。

如何以编程方式完成这项工作?

【问题讨论】:

    标签: c# wpf silverlight xaml data-binding


    【解决方案1】:

    如果您知道如何处理 XAML,代码中的格式总是相同的,并且只是非逐字翻译。

    <ListBox local:Attached.Test="{Binding PathToProperty)"/>
    
    var binding = new Binding("PathToProperty");
    listBox.SetBinding(Attached.TestProperty, binding);
    

    如果您在绑定上设置其他属性,例如 ElementName,您应该在 SetBinding 之前设置这些属性。 (这个SetBinding方法只是为了方便(如果你只设置Binding.Path甚至还有another one),对于非FrameworkElements你需要BindingOperations.SetBinding

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 2011-11-18
      • 2012-03-21
      • 1970-01-01
      相关资源
      最近更新 更多