当页面的datacontext用于其他绑定时,如何绑定到WPF依赖项属性?

当页面的datacontext用于其他绑定时,如何绑定到WPF依赖项属性?,wpf,data-binding,dependency-properties,Wpf,Data Binding,Dependency Properties,当页面的datacontext用于其他绑定时,如何绑定到WPF依赖项属性?(简单问题)在XAML中: Something="{Binding SomethingElse, ElementName=SomeElement}" 代码: BindingOperations.SetBinding(obj, SomeClass.SomethingProperty, new Binding { Path = new PropertyPath(SomeElementType.SomethingElseP

当页面的datacontext用于其他绑定时,如何绑定到WPF依赖项属性?(简单问题)

在XAML中:

Something="{Binding SomethingElse, ElementName=SomeElement}"
代码:

BindingOperations.SetBinding(obj, SomeClass.SomethingProperty, new Binding {
  Path = new PropertyPath(SomeElementType.SomethingElseProperty),  /* the UI property */
  Source = SomeElement /* the UI object */
});

尽管通常情况下,您会以相反的方式执行此操作,并将UI属性绑定到自定义依赖项属性。

需要设置元素的datacontext

XAML:

另见此相关问题:


在搜索Google搜索“WPF数据绑定”时,您是否要求提供其他内容?我正在寻找一个工作示例,其中页面的datacontext已用于其他绑定。事实证明,需要将元素的datacontext设置为页面本身。如果需要,我会发布代码并编辑问题。每当
Test
通过代码更改时,这会自动提供更改通知吗?你是我的救世主,托马斯·布拉特我可以用用户控制来完成吗?我试过同样的方法,但什么也没发生(也许我错过了什么.)。。
<Window x:Class="WpfDependencyPropertyTest.Window1" x:Name="mywindow">
   <StackPanel>
      <Label Content="{Binding Path=Test, ElementName=mywindow}" />
   </StackPanel>
</Window>
public static readonly DependencyProperty TestProperty =
        DependencyProperty.Register("Test",
                                    typeof(string),
                                    typeof(Window1),
                                    new FrameworkPropertyMetadata("Test"));
public string Test
{
   get { return (string)this.GetValue(Window1.TestProperty); }
   set { this.SetValue(Window1.TestProperty, value); }
}