i have made program several pages. simple program, have @ top area labels @ page are. every page new label defined. labels defined in dockpanel.xaml later included mainwindow.xaml.
i made current page label in different color.
my code:
my dockpanel.xaml first label (others same number change)
<label name="label1" foreground="{binding path=label1.color}" content="welcome" grid.column="0" horizontalalignment="left" fontsize="20" fontweight="light" fontstyle="italic"/> my dockpanelviewmodel
public class dockpanelviewmodel : viewmodelbase { #region member fields #endregion #region constructors /// <summary> /// default constructor /// </summary> public dockpanelviewmodel() { } #endregion #region properties protected brush _color; public brush color { { return _color; } set { _color = value; notifypropertychanged("color"); } } #endregion } later definitions of viewmodel 1 of page:
label1.color = system.windows.media.brushes.yellow; the point code dont want change color , dont know wrong :)
please help. thanks!
added .. pageviewmodelbase
public virtual dockpanelviewmodel label1 { { if (_label1 == null) { _label1 = new dockpanelviewmodel() { //text = "back", color = system.windows.media.brushes.yellow, }; } return _label1; } set { _label1 = value; notifypropertychanged("label1"); } }
it's getting worse updated question. please optimize , fix namings!
to bring work, here's suggestion:
- your dockpanelviewmodel seems ok
- create instance of dockpanelviewmodel , assign datacontext of view
- bind foreground property of label {binding path=color} (which color property of viewmodel
- remove "name" of label (you don't need in proper mvvm
- whenever want change color of label, change color property if viewmodel-instance (the 1 assigned view's datacontext)
- i have no idea public virtual dockpanelviewmodel label1 added question is. me seems unnecessary, delete it.
here's working example: view:
<grid> <grid.columndefinitions> <columndefinition width="*"/> <columndefinition width="*"/> </grid.columndefinitions> <label grid.column="0" foreground="{binding path=labelcolor}" content="welcome" fontsize="20" fontweight="light" fontstyle="italic"/> <stackpanel grid.column="1"> <button content="red" width="75" command="{binding changecolorcommand}" commandparameter="#ff0000"/> <button content="green" width="75" command="{binding changecolorcommand}" commandparameter="#00ff00" /> </stackpanel> </grid> view-code:
public mainwindow() { initializecomponent(); var vm = new viewmodel(); datacontext = vm; } viewmodel:
public class viewmodel : inotifypropertychanged { public icommand changecolorcommand { get; set; } protected brush _color; public brush labelcolor { { return _color; } set { _color = value; onpropertychange(); } } public viewmodel() { labelcolor = brushes.yellow; changecolorcommand = new relaycommand((o) => { labelcolor = new brushconverter().convertfromstring(o.tostring()) solidcolorbrush; }); } public event propertychangedeventhandler propertychanged; private void onpropertychange([callermembername] string propertyname = null) { var handler = propertychanged; if (handler != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } } relaycommand well-known standard class found everywhere on web.
Comments
Post a Comment