Wednesday, 15 September 2010

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


I want to add my DataGrid underlying scroll to the bottom as new items ObservableCollection . To achieve this, I ICommand but "

The way in reverse"
  public interface IViewModelCommand {void execute (the object to make a similar interface Parameter). }  

implementation

  action for the public square ViewModelRelayCommand: IViewModelCommand {private read-only & lt; Object & gt; _action; Public ViewModelMelmel Command (Action & Lieutenant; Object> Action) {If (Action == empty) Throw new logic ("Action"); _action = action; } Public Zero executed (object parameter) {_action (parameter); }}  

My View Module

  Private IViewModelCommand _scrollAction; Public IViewModelCommand ScrollAction {get {return_scrollAction; } Set {if (_scrollAction == value) returns; _scrollAction = value; OnPropertyChanged (); }}  

Then I scroll to the end will create treats for your Detagrid code from ()

  public sealed class Detagridbihaivr behavior & lt ; DataGrid & gt; {Public static DependencyProperty ScrollToEndProperty = DependencyProperty.Register ( "ScrollToEnd", typeof (IViewModelCommand), typeof (DataGridBehavior), New FrameworkPropertyMetadata (zero, FrameworkPropertyMetadataOptions.None)) read-only; Public IViewModelCommand ScrollToEnd {get {return (IViewModelCommand) GetValue (ScrollToEndProperty); } Set {Set Value (ScrollToEndProperty, Value); }} Safe Override ZeroOn (Attached) {base.OnAttached (); ScrollToEnd = New ViewModelRelayCommand (Scroll); } Secure override zero onducting () {base.OnDetaching (); ScrollToEnd = Faucet; } Private Zero Scroll (object parameter) {var mainDataGrid = AssociatedObject; If (MaindataGrid.Emms.Count> 0) {var border = VisualTreeHelper.GetChild (MainDataGrid, 0) as Decorator; If (range! = Zero) {var scroll = border. Children as ScrollViewer; Scroll (scroll! = Null). ScrollToEnd (); }}}}  

and attach it to my datagrid

  & lt; I: interaction.bihris & gt; & Lt; Local: Data GridWives ScolotOvd = "{Binding Scroll Action, Mode = Vanessesource}" /> & Lt; / I: Interaction.Behaviors & gt;  

Then just call if I _scrollAction = null!) _scrollAction.Execute (zero) from my ViewModel; Scroll to my grid and it works very well for <. / P>

My question is, is this a violation of MVVM?

"itemprop =" text ">

Just a little bit ...

MVVM worst practices in my experience Is practiced in the brain as a guideline. When you do not need the actual programming work, Mvivim is no use in finding solutions to keep straight, especially if you are working on solutions and running

But what you command approach Instead of thinking about an event?

In that case it may be useful to you:


No comments:

Post a Comment