2009-04-21 1 views
17

ページのdatacontextが他のバインディングに使用されているときに、WPF依存関係プロパティにバインドする方法はありますか? XAMLで(素朴な疑問)ページのdatacontextが他のバインディングに使用されているときに、WPF依存関係プロパティにバインドする方法はありますか?

+1

あなたは何を見つける以外の何かを求めている:

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); } } 

また、この関連の質問を参照"WPF databinding"のために? –

+3

他のバインディングにすでに というページのdatacontextが使用されている実例を探していました。わかるように、要素のdatacontextはページ自体に設定する必要がありました。私はコードを投稿し、必要に応じて質問を編集します。 –

答えて

31

設定が必要な要素のdatacontext。

XAML:

<Window x:Class="WpfDependencyPropertyTest.Window1" x:Name="mywindow"> 
    <StackPanel> 
     <Label Content="{Binding Path=Test, ElementName=mywindow}" /> 
    </StackPanel> 
</Window> 

C#の:あなたがGoogleで検索するとき

WPF DependencyProperties

+3

+1ありがとうございます。最後に理解しやすい、明確な例! –

+0

これは、 'Test'がコードによって変更されるたびに変更通知を自動的に提供しますか? – Kos

+0

あなたは私の救い主、トーマスブラット – user1836155

8

:コードで

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プロパティをバインドしますけれども。

関連する問題