c# - Logical Children of TabControl with bound TabItems -
i have tabcontrol
it's itemssource
bound observablecollection<string>
. in case, tabcontrol
has no logical children (logicaltreehelper.getchildren(tabctrl)
returns empty list).
if add tabitem
manually tabcontrol.items
collection, tabitem
logical child of tabcontrol.
why these ways behave differently? shouldn't tabcontrol
have logical child in both scenarios?
example code:
xaml
<window x:class="wpfapplication29.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <stackpanel> <tabcontrol name="tabctrl"/> <button content="count children" click="button_click_2"/> </stackpanel> </window>
code behind
using system.collections.objectmodel; using system.linq; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.media; namespace wpfapplication29 { public partial class mainwindow : window { public observablecollection<string> tabitems { { return (observablecollection<string>)getvalue(tabitemsproperty); } set { setvalue(tabitemsproperty, value); } } public static readonly dependencyproperty tabitemsproperty = dependencyproperty.register("tabitems", typeof(observablecollection<string>), typeof(mainwindow), new propertymetadata(null)); public mainwindow() { initializecomponent(); tabitems = new observablecollection<string>(); tabitems.add("foo"); //scenario 1 (visual children count: 1, logical children count: 0) tabctrl.setbinding(tabcontrol.itemssourceproperty, new binding("tabitems") { source = }); //scenario 2 (visual children count: 1, logical children count: 1) //tabctrl.items.add(new tabitem() { header = "bar", content = "bar" }); } private void button_click_2(object sender, routedeventargs e) { var visualchildrencount = visualtreehelper.getchildrencount(tabctrl); var logicalchildrencount = logicaltreehelper.getchildren(tabctrl).cast<object>().count(); messagebox.show(string.format("visual children: {0}, logical children: {1}", visualchildrencount, logicalchildrencount)); } } }
if add control usercontrol, or page, added logicaltree. however, uielements control's template not part of logicaltree.
imho, when add tabitem directly tabcontrol, expect present in logicaltree, intuitively. have direcly added there.
but when items generated itemssource, tabitems generated control's internal logic , not added logicaltree.
maybe better example listbox, or datagrid. imagine have itemssource bound large collection , need enable virtualization. items (uielements) generated, when needed (they in visible area of scrollviewver). if in logical tree, logical tree changing while scrolling. scrolling more visual "ui logic".
this article helps understand logical tree little bit better: http://www.codeproject.com/articles/21495/understanding-the-visual-tree-and-logical-tree-in
Comments
Post a Comment