是否从ViewModel发送命令到View违反MVVM?

时间:2015-03-17 04:06:06

标签: c# wpf mvvm datagrid

我希望我的DataGrid滚动到底部,因为新项目已添加到基础ObservableCollection。为了实现这一点,我创建了一个类似于ICommand的界面,但在"反向"方式。

public interface IViewModelCommand
{
    void Execute(object parameter);
}

实施

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);
    }
}

我的ViewModel

    private IViewModelCommand _scrollAction;
    public IViewModelCommand ScrollAction
    {
        get { return _scrollAction; }
        set
        {
            if (_scrollAction == value)
                return;
            _scrollAction = value;OnPropertyChanged();
        }
    }

然后我为我的DataGrid创建行为。 (滚动到取自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
        {
            get { 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) as Decorator;
                if (border != null)
                {
                    var scroll = border.Child as ScrollViewer;
                    if (scroll != null) scroll.ScrollToEnd();
                }
            }
        }
    }

并将其附加到我的DataGrid

        <i:Interaction.Behaviors>
            <local:DataGridBehavior ScrollToEnd="{Binding ScrollAction, Mode=OneWayToSource}" />
        </i:Interaction.Behaviors>

然后,从我的ViewModel,我只需要调用if (_scrollAction != null) _scrollAction.Execute(null);来滚动我的网格,它的效果非常好。

我的问题是,这是违反MVVM的吗?

1 个答案:

答案 0 :(得分:2)

一点点......

根据我的经验,MVVM作为一个粗略的指导方针是最健康的实践。当您的实际编程任务不需要时,特别是如果您正在运行解决方案,那么找到解决方案以保持MVVM是没有用的。

但你想过一个事件而不是命令方法吗?

在这种情况下,这可能对您有用:How can I Have a WPF EventTrigger on a View trigger when the underlying Viewmodel dictates it should?