Note that there are some explanatory texts on larger screens.

plurals
  1. POdesign pattern problem
    primarykey
    data
    text
    <p>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..</p> <p>The application is a dynamic application and at runtime I can add/remove usercontrols...</p> <p>So I have a MainWindowView that has behind a MainWindowModel.</p> <p>The MainWindowModel has a list of Workspaces witch are in fact WorkspaceModel classes...</p> <p>I have multiple UserControls and everyone off them has his own view model witch inherits WorkspaceModel. </p> <p>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).</p> <p>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...</p> <p>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... </p> <p>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?</p> <p>I hope it makes sense.....</p> <p>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</p> <p>First of all</p> <pre><code>public class MainWindowModel : ModelBase { private ObservableCollection&lt;WorkspaceModel&gt; _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&lt;WorkspaceModel&gt; Workspaces { get { if (_workspaces == null) { _workspaces = new ObservableCollection&lt;WorkspaceModel&gt;(); } return _workspaces; } } public ModelBase UserViewModel { get { return _userViewModel; } set { if (_userViewModel == value) { return; } _userViewModel = value; OnPropertyChanged("UserViewModel"); } } } </code></pre> <p>snippets from MainappView</p> <pre><code> &lt;DataTemplate x:Key="WorkspaceItemTemplate"&gt; &lt;Grid &gt; //workaround to use Type as in WPF &lt;Detail:DetailsViewSelector Content="{Binding}" TemplateType="{Binding}" &gt; &lt;Detail:DetailsViewSelector.Resources&gt; &lt;DataTemplate x:Key="TreeControlModel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &gt; &lt;TreeControl:TreeControlView /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="GridControlModel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &gt; &lt;GridControl:GridControlView /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="EmptyTemplate"&gt; &lt;/DataTemplate&gt; &lt;/Detail:DetailsViewSelector.Resources&gt; &lt;/Detail:DetailsViewSelector&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="WorkspacesTemplate"&gt; &lt;ItemsControl IsTabStop="False" ItemsSource="{Binding}" ItemTemplate="{StaticResource WorkspaceItemTemplate}" Margin="6,2"/&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; &lt;toolkit:HeaderedContentControl Grid.Column="2" HorizontalAlignment="Stretch" Name="hccWorkspaces" VerticalAlignment="Top" Header="Workspaces" Content="{Binding Source={StaticResource vm}, Path=Workspaces}" ContentTemplate="{StaticResource WorkspacesTemplate}"/&gt; 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&lt;TreeItem&gt; _items; TreeItem _selectedItem; #endregion // Fields #region Constructor public GridControlModel() { base.DisplayName = "GridControlModel"; } #endregion // Constructor #region Public Interface public ObservableCollection&lt;TreeItem&gt; 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 } </code></pre> <p></p> <pre><code> &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;sdk:DataGrid Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" ItemsSource="{Binding Path=SelectedTreeNode.Children}" IsEnabled="{Binding}" AutoGenerateColumns="False"&gt; &lt;sdk:DataGrid.Columns&gt; &lt;sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Id" Width="Auto" Binding="{Binding Id}" /&gt; &lt;sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Name" Width="Auto" Binding="{Binding Name}"/&gt; &lt;/sdk:DataGrid.Columns&gt; &lt;/sdk:DataGrid&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload