c# - How to style auto-generated elements in xaml -
i'm using extension this toolkit, automatically adds textbox
top of each datagrid column filtering following code.
<datagrid columnheaderstyle="{staticresource {componentresourcekey typeintargetassembly={x:type filter:datagridheaderfiltercontrol}, resourceid=datagridheaderfiltercontrolstyle}}" >
however, added column header , textboxes have different style want.
so can style column header, won't change textboxes.
<style x:key="filterheader" basedon="{staticresource {componentresourcekey typeintargetassembly={x:type filter:datagridheaderfiltercontrol}, resourceid=datagridheaderfiltercontrolstyle}}" targettype="{x:type datagridcolumnheader}"> <setter property="foreground" value="blue" /> </style> ... <datagrid columnheaderstyle="{dynamicresource filterheader}">
i tried putting inwindow.resources
see if had effect on textboxes. changed other textboxes doesn't have effect on textboxes created extension.
<style targettype="textbox"> <setter property="foreground" value="blue" /> </style>
but no dice. thing i've found works this:
public void datagrid_onload(object sender, routedeventargs e) { ienumerable<system.windows.controls.textbox> collection = findvisualchildren<system.windows.controls.textbox>(titlebar); foreach (system.windows.controls.textbox tb in collection) { tb.foreground = brushes.blue; } } public static ienumerable<t> findvisualchildren<t>(dependencyobject depobj) t : dependencyobject { if (depobj != null) { (int = 0; < visualtreehelper.getchildrencount(depobj); i++) { dependencyobject child = visualtreehelper.getchild(depobj, i); if (child != null && child t) { yield return (t)child; } foreach (t childofchild in findvisualchildren<t>(child)) { yield return childofchild; } } } }
but seems garbage way of doing it. how can purely in xaml or @ least more cleanly in c#?
from looking through source code, noticed textbox
isn't textbox subclass of textbox. delaytextbox
so modify style target instead.
<style targettype="delaytextbox"> <setter property="foreground" value="blue" /> </style>
i haven't tried should work.
update
finally found issue. styles should defined within style of parent.
<style targettype="{x:type filter:datagridcolumnfilter}"> <style.resources> <style targettype="{x:type support:delaytextbox}"> <setter property="foreground" value="blue" /> <setter property="fontweight" value="bold"/> </style> <style targettype="{x:type combobox}"> <setter property="foreground" value="blue" /> <setter property="fontweight" value="bold"/> </style> </style.resources> </style>
i've added comboxbox since assume you'd want too. can take out setter bold needed .. using test.
Comments
Post a Comment