tags:

views:

71

answers:

3

This is the code-behind of my view:

using System.Windows.Controls;

namespace TestBoundTabControl.Views
{
    public partial class SmartFormView : UserControl
    {
        public SmartFormView()
        {
            InitializeComponent();
        }

        public void BeforeLoad()
        {
            MainTabControl.SelectedIndex = MainTabControl.Items.Count - 1;
        }
    }
}

But why can't I access the method "BeforeLoad()" from the view's presenter?

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using TestBoundTabControl.Views;

namespace TestBoundTabControl.Presenters
{
    public class SmartFormPresenter : PresenterBase
    {
        #region ViewModelProperty: SmartFormAreaPresenters
        private ObservableCollection<SmartFormAreaPresenter> _smartFormAreaPresenters = new ObservableCollection<SmartFormAreaPresenter>();
        public ObservableCollection<SmartFormAreaPresenter> SmartFormAreaPresenters
        {
            get
            {
                return _smartFormAreaPresenters;
            }

            set
            {
                _smartFormAreaPresenters = value;
                OnPropertyChanged("SmartFormAreaPresenters");
            }
        }
        #endregion

          public SmartFormPresenter()
        {
            View = new SmartFormView();
            View.DataContext = this;


            for (int i = 0; i < 5; i++)
            {
                SmartFormAreaPresenters.Add(new SmartFormAreaPresenter());
            }

            View.BeforeLoad(); //method not found

        }
    }
}
+1  A: 

My guess is that the property View has type UserControl and not SmartFormView. If that is true you will have to cast it (or change it's type):

((SmartFormView) View).BeforeLoad();
Martin Liversage
A: 

View is obviously of some base type, like FrameworkElement. Try this code:

SmartFormView myView = new SmartFormView();

View = myView;

myView.BeforeLoad();
kek444
A: 

You don't show your PresenterBase class, but the PresenterBase.View property probably isn't of type SmartFormView. I'm not sure what type it is, but I'm guessing UserControl or one of its ancestors.

Here are some options:

  1. Make a base class for all of your Views, put a virtual BeforeLoad method on that base class, and make your PresenterBase's View property be of that type.
  2. Insert a typecast, as Martin suggested (this is more of a hack than a solution, IMHO).
  3. Make your base class generic on the view type, so that in SmartFormPresenter, the View property really can be of type SmartFormView. E.g.:

    public class PresenterBase<T> {
        ...
        public T View { get; set; }
        ...
    
Joe White