0

Im migrating an application from Windows Forms to WPF, so I am trying to use the MVVM pattern, but it is getting a bit tricky to me.

In Windows forms I have a form which contains a progressBar which has defined some new methods that allow me to perform steps, set its value, etc. This code is placed in the progressBar's code behind as below:

public class MyProgressBar : ProgressBar
{
    public MyProgressBar ()
    {
        InitializeComponent();
    }

    public void Method01()
    {...}

    public void Method02()
    {...}

    public void Method03()
    {...}
}

The code of the Method01, Method02 and Method03 is used in the container form, so, here is my question: In WPF, since I use the MVVM pattern, Where do I have to place that code??

Until now I have this classes:

My model:

public class MyFormModel : DataTable
{
    public OutputResultsDataTable()
        : base()
    {
        Columns.Add(new DataColumn("Number"));
        Columns.Add(new DataColumn("Message"));
        Columns.Add(new DataColumn("Detail"));
    }
}

My ViewModel:

public class MyNewWPFForm
{
    /// <summary>
    /// 
    /// </summary>
    public MyFormModel MyData{ get; set; }

    /// <summary>
    /// Constructor
    /// </summary>
    public MyNewWPFForm()
    {
        if (System.Windows.Application.Current.MainWindow != null)
        {
            MyData= new MyFormModel ();                
        }
    }
}

In WPF I have already created my new progressBar control and its container form, even I have been reading about MVVM but I cant understand where do I have to put the code of the Method0X, because, according to what I have read, I would have to put that code in the viewModel, but, here is another problem: How could I have access to my progress bar from my view model since it is inside another form??

Hope someone can help me. Thanks in advance.

Dante
  • 3,208
  • 9
  • 38
  • 56
  • What are Method01,2,3 supposed to do? – Dave Hillier May 31 '12 at 22:42
  • 1
    I think this post will be of benefit to you: http://stackoverflow.com/questions/1619505/wpf-openfiledialog-with-the-mvvm-pattern – dugas May 31 '12 at 22:44
  • @DaveHillier, Since I have a listView either Method0X allow me to insert rows directly, copy content to clipboard, perform an step in the progressBar, etc – Dante May 31 '12 at 22:47

1 Answers1

2

It sounds like the methods on your control are used for setting state of your control. You could use dependency properties to do this.

You use binding to connect your view to your view model.

Create a property for your controls progress.

Then in Xaml add a binding

<MyProgressBar Progress="{Binding CurrentProgress}" />

Set the data context of your view to a new view model. CurrentProgress is a property on the view model. Implement INotifyPropertyChanged on your view model.

For adding and removing things to/from your control you might ObservableCollection.

Dave Hillier
  • 18,105
  • 9
  • 43
  • 87