Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to an ancestor in WPF

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());      } } 
like image 248
Tedford Avatar asked Aug 04 '10 13:08

Tedford


People also ask

What is relative binding in WPF?

The RelativeSource is a markup extension that is used in particular binding cases when we try to bind a property of a given object to another property of the object itself, when we try to bind a property of a object to another one of its relative parents, when binding a dependency property value to a piece of XAML in ...

What is binding path in WPF?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.

Which class is used for data binding in WPF?

WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.


1 Answers

the following should work :

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,                                                          AncestorType={x:Type Window}},                                                          Path=DataContext.Client.Name}" /> 
like image 81
decyclone Avatar answered Oct 07 '22 10:10

decyclone