c# - want to change window color using XAML RESOURCE FILE -
i want change color of window applying style defined in xaml resource file. have created style target type set window no key (assuming applied automatically windows). style not applied windows in app. using below code works controls in window,but not changing color of window itself. please let me know wrong.if give target type grid changes color, if give window not changing color
resource file
<color x:key="mainbackgroundcolor"></color> <solidcolorbrush x:key="mainbackground" color="{binding path=datacontext.mainapplicationcolor, updatesourcetrigger=propertychanged, relativesource={relativesource ancestortype={x:type window}, mode=findancestor}, fallbackvalue={staticresource mainbackgroundcolor}}"/> <style targettype="window"> <setter property="background" value="{staticresource mainbackground}" /> </style>
view
<window x:class="mvvmlight1.view.mainmenuview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ignore="http://www.ignore.com" mc:ignorable="d ignore" name="x_main" > <window.resources> <resourcedictionary> <resourcedictionary.mergeddictionaries> <resourcedictionary source="/colorresources.xaml"/> </resourcedictionary.mergeddictionaries> </resourcedictionary> </window.resources> <grid height="60" width="60"> </grid> </window>
view model*
public string changecolorcommandtext { get; set; } private relaycommand m_cmdchangecolor; public icommand changecolor { { return m_cmdchangecolor ?? (m_cmdchangecolor = new relaycommand(changecoloraction, () => true)); } } private void changecoloraction() { mainapplicationcolor = (color)colorconverter.convertfromstring("#4484f3"); }
the reason why target type not applied window because, here using derived type of window name "mainmenuview". targettype in styles doesn't manage derived types. in style resource have set target type derived type (mainmenuview). doing applied mainmenuview window.
xmlns:local="yournamespace:yourapplication" <style targettype="local:mainmenuview"> <setter property="background" value="green" /> </style>
to apply style windows in app, necessary have set x:key defined style , reference style windows either in xaml or in code behind.
Comments
Post a Comment