wpf - Adding custom/existing FrameworkElements to ItemsControl -
when add overridden frameworkelement
or existing ui control itemscontrol
, following binding error.
system.windows.data error: 25 : itemtemplate , itemtemplateselector not valid item.; type='button' system.windows.data error: 25 : itemtemplate , itemtemplateselector not valid item.; type='frameworkelement'
how should handling mixed uielement
/non uielement
types itemscontrol
?
namespace wpfapplication27 { class data { public string type { get; set; } }; public partial class mainwindow : window { public mainwindow() { initializecomponent(); m_itemscontrol.items.add(new data() { type = "a" }); m_itemscontrol.items.add(new data() { type = "b" }); m_itemscontrol.items.add(new data() { type = "a" }); m_itemscontrol.items.add(new button()); m_itemscontrol.items.add(new frameworkelement()); } } public class customdatatemplateselector : datatemplateselector { public override datatemplate selecttemplate(object _item, dependencyobject _container) { var container = _container frameworkelement; var data = _item data; if (data.type == "a") { return container.findresource("adatatemplate") datatemplate; } else if (data.type == "b") { return container.findresource("bdatatemplate") datatemplate; } return null; } } } <window x:class="wpfapplication27.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication27"> <window.resources> <datatemplate x:key="adatatemplate"> <ellipse width="5" height="5" fill="red"/> </datatemplate> <datatemplate x:key="bdatatemplate"> <rectangle width="5" height="5" fill="blue"/> </datatemplate> </window.resources> <itemscontrol name="m_itemscontrol"> <itemscontrol.itemtemplateselector> <local:customdatatemplateselector/> </itemscontrol.itemtemplateselector> <itemscontrol.template> <controltemplate> <canvas isitemshost="true"/> </controltemplate> </itemscontrol.template> </itemscontrol> </window>
Comments
Post a Comment