views:

132

answers:

1

Hello I have a application design problem and I home you can help me solve it.... This is my first application in silverlight and the first application using mvvm design pattern and I am not sure I am applying mvvm how I am supposed to..

The application is a dynamic application and at runtime I can add/remove usercontrols...

So I have a MainWindowView that has behind a MainWindowModel.

The MainWindowModel has a list of Workspaces witch are in fact WorkspaceModel classes...

I have multiple UserControls and everyone off them has his own view model witch inherits WorkspaceModel.

The Workspaces property is binded to a container in MainWindowView so adding to the Workspaces list a new UserControlModel will automatically add that control to the view. Now where is my problem... I want to make this dynamically added usercontrols to interact. Lets say one user control is a tree and one is a grid... I want a method to say that Itemsource property of Grid UserControl Model (WorkspaceModel) to be binded to SelectedNode.Nodes Property from the Tree Usercontrol Model (WorkspaceModel).

The MainWindowModel has a property name BindingEntries witch has a list of BindingEntry... BindingEntry stores the source property and the destination property of the binding like my workspacemodel_1.SelectedNode.Nodes -> workspacemodel_2.ItemSource...

Or as a variation the MainWindowView has a property ViewStateModel. This ViewStateModel class has dynamic created properties - "injected" with property type descriptors/reflections etc... So the user can define at run time the displayed usercontrols (by modifying the Workspaces list) and can define a view model (the ViewStameModel) and the binding is between workspacemodel properties and this ViewStateModel properties...

So I actually want to bind 2 view models one to another... How to do that? Create an observer pattern? Is the design until now totally wrong?

I hope it makes sense.....

I will try to add some sample code...the project is quite big I will try co put only the part I have mentioned in the problem desciption... I hope I will not miss any pice of code

First of all

public class MainWindowModel : ModelBase
    {
        private ObservableCollection<WorkspaceModel> _workspaces;
        private ModelBase _userViewModel;
  public MainWindowModel()
        {
            base.DisplayName = "MainWindowModel";
            ShowTreeView(1);
            ShowTreeView(2);
            ShowGridView(3);
            ShowGridView(4);
            UserViewModel = new ViewModel(); //this is the ViewStateModel
        }
   void ShowTreeView(int id)
        {
            WorkspaceModel workspace = ControlFactory.CreateModel("TreeControlModel", id);
            this.Workspaces.Add(workspace);
            OnPropertyChanged("Workspaces");
            SelectedWorkspace = workspace;
        }
        void ShowGridView(int id)
        {
            WorkspaceModel workspace = ControlFactory.CreateModel("GridControlModel", id);
            this.Workspaces.Add(workspace);
            OnPropertyChanged("Workspaces");
            SelectedWorkspace = workspace;
        }
 public ObservableCollection<WorkspaceModel> Workspaces
        {
            get
            {
                if (_workspaces == null)
                {
                    _workspaces = new ObservableCollection<WorkspaceModel>();
                }
                return _workspaces;
            }
        }
  public ModelBase UserViewModel
        {
            get
            {
                return _userViewModel;
            }
            set
            {
                if (_userViewModel == value)
                {
                    return;
                }
                _userViewModel = value;

                OnPropertyChanged("UserViewModel");
            }
       } 

}

snippets from MainappView

 <DataTemplate x:Key="WorkspaceItemTemplate">
            <Grid >

              //workaround to use Type as in WPF
                <Detail:DetailsViewSelector Content="{Binding}"  TemplateType="{Binding}"  >
                    <Detail:DetailsViewSelector.Resources>
                        <DataTemplate x:Key="TreeControlModel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  >
                            <TreeControl:TreeControlView />
                        </DataTemplate>
                        <DataTemplate x:Key="GridControlModel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  >
                            <GridControl:GridControlView />
                        </DataTemplate>
                        <DataTemplate x:Key="EmptyTemplate">
                        </DataTemplate>
                    </Detail:DetailsViewSelector.Resources>
                </Detail:DetailsViewSelector>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="WorkspacesTemplate">
            <ItemsControl IsTabStop="False" ItemsSource="{Binding}" ItemTemplate="{StaticResource WorkspaceItemTemplate}" Margin="6,2"/>
        </DataTemplate>
    </UserControl.Resources>

<toolkit:HeaderedContentControl Grid.Column="2" HorizontalAlignment="Stretch" Name="hccWorkspaces" VerticalAlignment="Top"  Header="Workspaces"  Content="{Binding Source={StaticResource vm}, Path=Workspaces}" ContentTemplate="{StaticResource WorkspacesTemplate}"/>





public class ControlFactory
        {
            public static WorkspaceModel CreateModel(string type, int id) 
            {
                switch (type) 
                {
                    case "TreeControlModel": return new TreeControlModel() { Id=id}; break;
                    case "GridControlModel": return new GridControlModel() { Id = id }; break;
                }
                return null;
            }
        }









public class GridControlModel : WorkspaceModel
    {
        #region Fields
        ObservableCollection<TreeItem> _items;
        TreeItem _selectedItem;
        #endregion // Fields

        #region Constructor

        public GridControlModel()
        {

            base.DisplayName = "GridControlModel";
        }
        #endregion // Constructor

        #region Public Interface

        public ObservableCollection<TreeItem> Items
        {
            get
            {
                return _items;
            }
            set
            {
                if (_items == value)
                    return;
                _items = value;
                OnPropertyChanged("Items");
            }
        }
        public TreeItem SelectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                if (_selectedItem.Equals(value))
                {
                    return;
                }
                _selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        #endregion // Public Interface

        #region  Base Class Overrides

        protected override void OnDispose()
        {
            this.OnDispose();
        }

        #endregion // Base Class Overrides
    }

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid  Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" ItemsSource="{Binding Path=SelectedTreeNode.Children}" IsEnabled="{Binding}" AutoGenerateColumns="False">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Id" Width="Auto" Binding="{Binding Id}" />
                <sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Name" Width="Auto" Binding="{Binding Name}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    </Grid>
</UserControl>
A: 

For communicating between your view models I would suggest taking a look at the Messenger implementation in MVVM Light as a simple solution.

Alternatively, the Mediator Pattern as described here might be interesting: http://marlongrech.wordpress.com/2008/03/20/more-than-just-mvc-for-wpf/

Scrappydog