c# - Is send command from ViewModel to View violating MVVM? -


i want datagrid scroll bottom new item added underlying observablecollection. achieve this, make interface similar icommand in "reverse" way.

public interface iviewmodelcommand {     void execute(object parameter); } 

implementation

public class viewmodelrelaycommand : iviewmodelcommand {     private readonly action<object> _action;     public viewmodelrelaycommand(action<object> action)     {         if(action == null)             throw new argumentnullexception("action");          _action = action;     }      public void execute(object parameter)     {         _action(parameter);     } } 

my viewmodel

    private iviewmodelcommand _scrollaction;     public iviewmodelcommand scrollaction     {         { return _scrollaction; }         set         {             if (_scrollaction == value)                 return;             _scrollaction = value;onpropertychanged();         }     } 

then create behavior datagrid. (scroll end code taken here)

public sealed class datagridbehavior : behavior<datagrid> {         public static readonly dependencyproperty scrolltoendproperty =             dependencyproperty.register (                 "scrolltoend",                 typeof(iviewmodelcommand),                 typeof(datagridbehavior),                 new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.none)             );          public iviewmodelcommand scrolltoend         {             { return (iviewmodelcommand)getvalue(scrolltoendproperty); }             set { setvalue(scrolltoendproperty, value); }         }          protected override void onattached()         {             base.onattached();             scrolltoend = new viewmodelrelaycommand(scroll);         }          protected override void ondetaching()         {             base.ondetaching();             scrolltoend = null;         }          private void scroll(object parameter)         {             var maindatagrid = associatedobject;             if (maindatagrid.items.count > 0)             {                 var border = visualtreehelper.getchild(maindatagrid, 0) decorator;                 if (border != null)                 {                     var scroll = border.child scrollviewer;                     if (scroll != null) scroll.scrolltoend();                 }             }         }     } 

and attach datagrid

        <i:interaction.behaviors>             <local:datagridbehavior scrolltoend="{binding scrollaction, mode=onewaytosource}" />         </i:interaction.behaviors> 

then viewmodel call if (_scrollaction != null) _scrollaction.execute(null); scroll grid , works well.

my question, violating mvvm?

just little bit...

in experience mvvm healthfully practiced rough guideline. there's no use in finding solutions keep mvvm straight when actual programming task doesn't require it, if got working solutions , running.

but have thought of event instead of command approach?

in case may useful you: how can have wpf eventtrigger on view trigger when underlying viewmodel dictates should?


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -