wpf - How to change all TextBox's Foreground Color in an application -
wpf - How to change all TextBox's Foreground Color in an application -
in app (c# wpf) have 30 or 40 textboxes in more grids , want alter foreground color in loop. utilize code below , works. want utilize whole project, not concrete grid
xaml code
<grid x:name"stk"> .... textboxes ... </grid> *.cs code
foreach (textbox item in this.stk.children.oftype<textbox>()) { if (item.name.startswith("txt")) item.foreground = brushes.orange; } so, when have more grids, have set x:name="..." each 1 , implies more foreach loops.
much simpler way
define style targettype set textbox , no key. way style applied textbox in application without need bind style or foreground each textbox.
<application.resources> <solidcolorbrush color="red" x:key="txtcolor" /> <style targettype="textbox"> <setter property="foreground" value="{dynamicresource txtcolor}" /> </style> </application.resources> to alter foreground color.
private void button_click(object sender, routedeventargs e) { if (application.current.resources.contains("txtcolor")) { application.current.resources["txtcolor"] = new solidcolorbrush(colors.blue); } } wpf
Comments
Post a Comment