wpf - Binding to property on root element in DataTemplate through a ContentControl -
in user interface want put titles above usercontrols.
i want declare these titles in xaml future localizability, want keep them out of datacontexts.
can databinding fetch them property set on root node of usercontrol?
i have boiled problem down following code example:
using system.windows; namespace wpfapplication12 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); this.person = new author { name = "guge" }; this.datacontext = this; } public object person { get; set; } } public class author { public string name { get; set; } } }
and:
<window x:class="wpfapplication12.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication12" title="mainwindow" height="350" width="525"> <window.resources> <datatemplate datatype="{x:type local:author}"> <border automationproperties.name="author" borderthickness="1" borderbrush="black"> <label content="{binding name}"/> </border> </datatemplate> </window.resources> <stackpanel> <label x:name="position" content="author"/> <contentcontrol x:name="presentation" content="{binding person}"/> </stackpanel>
and practical problem is: how can use databinding in content property of "position" label fetch word "author" automationproperties.name property of border in datatemplate?
how going route on data object:
public class author { public string name { get; set; } public string typename { get; set; } // might better in base class person }
and:
<window.resources> <datatemplate datatype="{x:type local:author}"> <border automationproperties.name="{binding typename}" borderthickness="1" borderbrush="black"> <label content="{binding name}"/> </border> </datatemplate> </window.resources> <stackpanel> <label x:name="position" content="{binding elementname=presentation, path=datacontext.typename}"/> <contentcontrol x:name="presentation" content="{binding person}"/> </stackpanel>
Comments
Post a Comment