Saturday, 15 May 2010

c# - Can't get the view updated, even though the model is already updated -



c# - Can't get the view updated, even though the model is already updated -

this xaml

<window x:class="notebox.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:notebox" title="notebox"> <window.resources> <local:noteoctaveconverter x:key="noteoctaveconverter"/> <local:dottednoteconverter x:key="dottednoteconverter"/> <local:noteenumerationconverter x:key="noteenumerationconverter"/> <local:noteaccidentalconverter x:key="noteaccidentalconverter"/> </window.resources> <window.inputbindings> <keybinding key="oemperiod" command="{binding keyboardhotkeycommand}" commandparameter="blank"/> <keybinding key="d0" command="{binding keyboardhotkeycommand}" commandparameter="rest"/> <keybinding key="d1" command="{binding keyboardhotkeycommand}" commandparameter="n1"/> <keybinding key="d2" command="{binding keyboardhotkeycommand}" commandparameter="n2"/> <keybinding key="d3" command="{binding keyboardhotkeycommand}" commandparameter="n3"/> <keybinding key="d4" command="{binding keyboardhotkeycommand}" commandparameter="n4"/> <keybinding key="d5" command="{binding keyboardhotkeycommand}" commandparameter="n5"/> <keybinding key="d6" command="{binding keyboardhotkeycommand}" commandparameter="n6"/> <keybinding key="d7" command="{binding keyboardhotkeycommand}" commandparameter="n7"/> </window.inputbindings> <grid x:name="grid" background="greenyellow"> <grid.columndefinitions> <columndefinition width="20"/> <columndefinition width="auto"/> <columndefinition width="20"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="20"/> <rowdefinition height="auto"/> <rowdefinition height="20"/> </grid.rowdefinitions> <textblock grid.column="0" grid.row="1" text="{binding path=musicalnotation.accidental, converter={staticresource noteaccidentalconverter}, mode=oneway}" fontsize="15" fontfamily="couriernew" horizontalalignment="center" verticalalignment="center"/> <textblock grid.column="1" grid.row="1" text="{binding path=musicalnotation.note, converter={staticresource noteenumerationconverter}, mode=oneway}" fontsize="15" fontfamily="couriernew" horizontalalignment="center" verticalalignment="center"/> <listbox grid.column="1" grid.row="0" borderbrush="transparent" background="transparent" itemssource="{binding path=musicalnotation.octave, converter={staticresource noteoctaveconverter}, converterparameter=top, mode=oneway}" horizontalalignment="center" verticalalignment="center"> <listbox.itemspanel> <itemspaneltemplate> <stackpanel isitemshost="true"/> </itemspaneltemplate> </listbox.itemspanel> </listbox> <listbox grid.column="1" grid.row="2" borderbrush="transparent" background="transparent" itemssource="{binding path=musicalnotation.octave, converter={staticresource noteoctaveconverter}, converterparameter=bot, mode=oneway}" horizontalalignment="center" verticalalignment="center"> <listbox.itemspanel> <itemspaneltemplate> <stackpanel isitemshost="true"/> </itemspaneltemplate> </listbox.itemspanel> </listbox> <listbox grid.column="2" grid.row="1" borderbrush="transparent" background="transparent" itemssource="{binding path=musicalnotation.dot, converter={staticresource dottednoteconverter}, mode=oneway}" horizontalalignment="center" verticalalignment="center"> <listbox.itemspanel> <itemspaneltemplate> <wrappanel isitemshost="true"/> </itemspaneltemplate> </listbox.itemspanel> </listbox> </grid>

this xaml.cs

public partial class mainwindow : window { public mainwindow() { initializecomponent(); datacontext = new noteboxviewmodel(); } }

and modelview

public class noteboxviewmodel { public musicalnotation musicalnotation { get; set; } public icommand keyboardhotkeycommand { get; private set; } public bool isselected { get; set; } public noteboxviewmodel() { musicalnotation = new musicalnotation(); keyboardhotkeycommand = new keyboardhotkeycommand(this); isselected = true; //initialization testing musicalnotation.note = notes.n1; musicalnotation.dot = 1; musicalnotation.octave = -2; musicalnotation.accidental = accidentals.flattened; } }

even though model updated, view isn't when input using inputbindings. input detection working , model updated properly. converter works well, otherwise, initialization testing won't visualize too. (the info initialization testing showed perfectly).

my guess somewhere on binding. can't find any.

thanks.

note

you have implement inotifypropertychanged

public class noteboxviewmodel:inotifypropertychanged { //here illustration public musicalnotation musicalnotation { get{ homecoming _musicalnotation; } set{ _musicalnotation =value; // using [callermembername] don't need pass name of method (this available in .net 4.5 ) notifypropertychanged();} } public noteboxviewmodel() { musicalnotation = new musicalnotation(); keyboardhotkeycommand = new keyboardhotkeycommand(this); isselected = true; musicalnotation.note = notes.n1; musicalnotation.dot = 1; musicalnotation.octave = -2; musicalnotation.accidental = accidentals.flattened; } public event propertychangedeventhandler propertychanged; private void notifypropertychanged([callermembername] string propertyname = "") { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } }

c# wpf xaml data-binding mvvm

No comments:

Post a Comment