3

I've tried to read article WPF/Silverlight: Step By Step Guide to MVVM but I can not understand it completely.

However I've noticied such guideline:

That is your View.xaml.cs that is supposed to have almost no code.

How should I fix my code below? Should I extract my WCF code to some another place? Thanks.

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        ChannelFactory<IManagementConsole> pipeFactory =
                new ChannelFactory<IManagementConsole>(
                    new NetNamedPipeBinding(),
                    new EndpointAddress(
                        "net.pipe://localhost/PipeManagementConsole"));

        IManagementConsole pipeProxy =
          pipeFactory.CreateChannel();

        List<ConsoleData> datas = new List<ConsoleData>();
        foreach (StrategyDescriptor sd in pipeProxy.GetStrategies())
        {
            datas.Add(pipeProxy.GetData(sd.Id));
        }
        dataGrid1.ItemsSource = datas;
    }
}
sll
  • 61,540
  • 22
  • 104
  • 156
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

1 Answers1

1

Yes, this is bad practice especially from the MVVM perspectives.

Extract all business logic into the ServiceViewModel class, in View just set instance of ViewModel to DataContext:

 public MainWindow()
 {
      InitializeComponent();
      this.DataContext = new ServiceViewModel();
 }

If you have an other class/window which is instantiate this Window you should set ViewModel within it. For instance:

MyWindow childWindow = new MyWindow();
childWindow.DataContext = new ServiceViewModel();

So now you can see MVVM in action, in MainWindow XAML file you can use bindings like below:

<!-- Considering that ServiceViewModel has 
     public string ServiceName property 
 -->
<TextBlock Text="{Binding ServiceName}" />

<!-- Considering that ServiceViewModel has
     public List<ConsoleData> DataItems property
 -->
<DataGrid ItemsSource="{Binding DataItems}" />

In this way your logic stay in ViewModel and decoupled from View.

PS:

I would suggest using ObservableCollection<ConsoleData> for ConsoleData list, benefits are: (MSDN)

ObservableCollection Class

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

sll
  • 61,540
  • 22
  • 104
  • 156
  • thanks, can I set DataContext in xaml and so eliminate this last line of code? How to implement ServiceViewModel? link to corresponding documentation is welcome. – Oleg Vazhnev Nov 11 '11 at 21:17
  • @javapowered : see just updated answer, basically your ViewModel should expose `ObservableCollection DataItems` then just bind it in XAML like shown in answer – sll Nov 11 '11 at 21:21
  • Yes, you can set the DataContext in XAML. You'll need to add a namespace reference and create an instance of the ViewModel class in the Resources section. Then you an bind the DataContext to that resource. – Joel Cochran Nov 14 '11 at 13:39