c# - Retrieving the TabItem hosting a nested WPF Control -
i have nested controls on wpf tabcontrol. need find specific tabitem parent of control. preferably using linq. ideas.
a non-selected tabitem don't exist in visual tree, in logical tree.
i've made small example on this:
<tabcontrol x:name="tabcontrol"> <tabitem header="test 1"> <button content="click tabitem name" margin="100" click="btngetparent_click"/> </tabitem> <tabitem header="test 2"/> </tabcontrol>
clicking on button inside tabitem should display tabitem's header meaning have access , able work need.
i've kept code pretty simple here too:
public partial class mainwindow : window { public mainwindow() { initializecomponent(); } private void btngetparent_click(object sender, routedeventargs e) { tabitem ti = tryfindparent<tabitem>(sender button); messagebox.show(ti.header.tostring()); } }
and helper method:
public t tryfindparent<t>(dependencyobject child) t : dependencyobject { dependencyobject parentobject = logicaltreehelper.getparent(child); if (parentobject == null) return null; t parent = parentobject t; if (parent != null) return parent; else return tryfindparent<t>(parentobject); }
the call logicaltreehelper instead of usual visualtreehelper job here.
give try, hope helps.
Comments
Post a Comment