绑定到WPF中的祖先 [英] Binding to an ancestor in WPF
问题描述
我在那有我要绑定到一类是Windows父母在DataContext的属性的一个属性值的TextBlock控件一个程序集的窗口。被用作在DataContext类仅在第二组件中定义的。我的问题是我需要什么类型指定在我发言的结合类型。我可以只使用在DataContext的属性的类型是两个组件之间常见的还是我需要使用的DataContext的类型?
下面是如何,我认为它应该工作的原型,但因为它不是我感到困惑的事:)
大会#1 结果
窗口
<的TextBlock
文本={绑定的RelativeSource = {的RelativeSource
AncestorType = {X:类型的客户端:客户端}},路径=名称}/>
大会#2 结果
应用壳牌
类壳
{
公共客户端{{返回客户端; } {设置客户端=价值; }}
OnStartup()
{
NavigationWindow窗口=新NavigationWindow();
window.DataContext =这一点;
window.Navigate(GetHomeView());
}
}
下面应该工作:
< TextBlock的文本={绑定的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:类型窗口}},路径= DataContext.Client.Name}/>
I have a window in one assembly that has a TextBlock control that I want to bind to the value of a Property of a class that is the property of the DataContext of that windows parent. The class that is serving as the DataContext is only defined within the second assembly. My question is what type do I need to specify as the Type in my binding statement. Can I just use the type of the DataContext's property that is common between the two assemblies or do I need to use the type of the DataContext?
The below is a prototype of how I think it should work but since it isn't I am confused about something :)
Assembly #1
Window
<TextBlock
Text="{Binding RelativeSource={RelativeSource
AncestorType={x:Type client:Client}}, Path=Name }"/>
Assembly #2
Application Shell
class Shell
{
public Client Client { get { return client; } set { client = value; } }
OnStartup()
{
NavigationWindow window = new NavigationWindow();
window.DataContext = this;
window.Navigate(GetHomeView());
}
}
the following should work :
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Client.Name}" />
这篇关于绑定到WPF中的祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!