在MVVM中,可以在视图的代码后面访问ViewModel吗? [英] In MVVM is it acceptable to access the ViewModel in the view's code behind?

查看:84
本文介绍了在MVVM中,可以在视图的代码后面访问ViewModel吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVVM模式中,是否可以接受甚至可以在后面的视图代码中访问ViewModel属性?

IN an MVVM pattern is it acceptable or even possible to access ViewModel properties in the views code behind?

我有一个可观察的集合,该集合可填充到ViewModel中。我需要在视图中使用它来绑定到带有链接列表的无尽报价器。即

I have an observable collection which is populated in the ViewModel. I need to use it in the view to bind to an endless ticker with a linked list. i.e.

    private LinkedList<Border> tickerForex = new LinkedList<Border>();

    public ForexBuy()
    {
        InitializeComponent();
        DataContext = new ForexViewModel();
    }

    private void InitializeForexTicker()
    {
        CanvasForexBuyTicker.Children.Clear();
        foreach (var currency in DataContext.Currencies) //Is this possible/allowable???
        {
           AddTickerItem(currency);
        }

        CanvasForexBuyTicker.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate
        { var node = tickerForex.First;

            while (node != null)
            {
                if (node.Previous != null)
                {
                    Canvas.SetLeft(node.Value, Canvas.GetLeft(node.Previous.Value) + node.Previous.Value.ActualWidth + gap);
                }
                else
                {
                    Canvas.SetLeft(node.Value, CanvasForexBuyTicker.Width + gap);
                }

                node = node.Next;
            }

            return null;

        }), null);

}

void AddTickerItem(Currency currency)
    {
        Border border = new Border();
        border.Background = new SolidColorBrush(Color.FromArgb(255, 0, 99, 99));

        if (currency.IsUpward == 0)
        {
            border.Background = new SolidColorBrush(Color.FromArgb(255, 255, 153, 0));
        }

        border.BorderThickness = new Thickness(3);
        border.BorderBrush = new SolidColorBrush(Colors.White);
        border.CornerRadius = new CornerRadius(10);
        border.Width = Double.NaN;
        border.Height = 35;

        UIHelper.CanvasAutoSize canvas = new UIHelper.CanvasAutoSize();
        canvas.Background = Brushes.Green;  
        canvas.Tag = currency;
        canvas.Height = Double.NaN;

        TextBlock tb = new TextBlock
        {
            Text = currency.Code + " " + currency.Sell + " ",
            FontSize = 22,
            FontWeight = FontWeights.Bold,
            Foreground = Brushes.Black
        };

        tb.SetValue(Canvas.LeftProperty, 8d);
        tb.SetValue(Canvas.TopProperty, 2d);
        canvas.Children.Add(tb);

        tb.TouchDown += TouchTickerItem;

        border.Child = canvas;

        CanvasForexBuyTicker.Children.Add(border);
        Canvas.SetTop(CanvasForexBuyTicker, 3);
        Canvas.SetLeft(CanvasForexBuyTicker, 0);

        tickerForex.AddLast(border);
    }

关于调度员是否应从ViewModel触发,我有些困惑或是否在后面的视图代码中使用它。

I'm a little lost as to whether the dispatcher should fire from the ViewModel or whether to use it in the views code behind.

推荐答案

如果问题只是关于如何从后面的代码访问ViewModel的问题,则只需将DataContext强制转换为适当的类型:

If the question is just about how to access the ViewModel from code behind, you could simply cast the DataContext to the proper type:

var viewModel = (MyViewModel)DataContext;

foreach (var currency in viewModel.Currencies)
{
    ...
}

是否可以接受这取决于口味。通过视图XAML中的绑定或背后的一段代码访问视图模型,我看不出任何根本区别。

If this is acceptable or not is a matter of taste. I do not see any fundamental difference in accessing the view model by bindings in the view's XAML or by a piece of code-behind.

这篇关于在MVVM中,可以在视图的代码后面访问ViewModel吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆