How do I bind UserControl to GridView while grouping? - xaml

I want to Bind UserControl in the ItemTemplate section of the GridView.
UserControl contains nothing but with decorated text..
I had been using Collection which consisted of and was binding Content directly within ItemTemplate within the main xaml page, which was easy.
But now my collection is changed to .. How do I bind now?

Don't put UserControls in the model (i.e. your collection). They belong in the view (i.e. your XAML page).
I'm guessing you replaced the String you used originally with a UserControl because you want to use a different UserControl for different items. Take a look at ItemTemplateSelector. You can bind it to a DataTemplateSelector inside which you can decide which template to use based on the individual item. You can now have a custom class for the Content and a property inside it to select the UserControl based on it.

Related

Use the same user control xaml with different view models in Prism

I have an abstract base view model (IBaseViewModel) and two classes that implement it (Base1ViewModel, Base2ViewModel). I also have a Xaml user control, and I want in one page to use Base1ViewModel as view model and in another page Base2ViewModel as view model.
Prism best practice is to bind to view model by name, so what I ended up doing is duplicating the xaml file into Base1View and Base2View so each view gets the right view model, but this is obviously not ideal because then changes to the xaml should be duplicated as well.
Is there a solution/design in which I can reuse the same xaml user control with two different view models?
Thanks,
Noam

Dynamically loaded html doesnt have access to viewModel

I will give a simple case scenario to illustrate my problem. I have a view and a viewModel. The viewModel has a method redirect(). After my view and its model are loaded I dynamically add a html button to the page and use click.delegate to link to method redirect() in viewModel. But clicking the button doesn't call the method. How to fix this.
If binding is not a possible workaround
You cannot add new bindings to the DOM through DOM manipulation because they will not be bound properly.
Instead you should either conditionally show it -
<button click.delegate="redirect()" if.bind="showRedirect">Redirect</button>

What is best technique for switching Xaml views?

I have "alternative" xaml views for a viewmodel and I am currently using a technique where I set the style property on the view (containing a control template) and would like to know what is the best/preferred Caliburn technique to re-invoke conventions, bindings, etc., that are setup initially by Caliburn when showing the viewmodel.
I am using a View-Model-First approach, so possibly I am using the wrong technique and should be looking at a way (e.g. View contexts) to have the Viewmodel instigate the change?
Thanks
<ContentControl cm:View.Context="{Binding CurrentView, Mode=TwoWay}" cm:View.Model="{Binding}" />
Multiple views over 1 viewmodel technique. Then the folder structure would be slightly different to help organize but namespace is how view is found to place into the contentcontrol content. I use this in multiple projects
Viewmodel logic facilitates the view "switching".

Passing control as command parameter in Silverlight and MVVM Light

I am trying to get my head around MVVM and the Navigation-based project template in SL4. At the moment I am trying to move the ContentFrame_Navigated event handler into the ViewModel. Basically this event handler checks each hyperlink button in the menu bar against the current page and adjusts the style accordingly. To do this it seems I need to pass the EventArgs as well as another object. I see MVVM Light has the PassEventArgsToCommand bit, but what about passing another object/control? In this case it's the StackPanel hosting the list of menu item hyperlinks. I'm just getting my head around the MVVM concept, what's the best practice in this case?
Cheers,
Dany.
With MVVM you have to think more abstract. You are not daling with a list of HyperLink elements, but with a list of Navigation Targets. I.e. you should separate the presentation (HyperLink elemen) from the data (the URL, the Title, whether the item is selected or not, etc.). You now hold the data in a list on your ViewModel (normally you would see this data as the ViewModel of your Hyperlinks, and name it accordingly). The items are held in a ObservableCollection so that you can track changes if a item is added etc.
To display this list you can use a class inheriting from ItemsPresnter (e.g. a ListBox) and use binding to set the properties. Now you navigate to a page you can set the IsSelected property of the relevant item in the list, resetting all other IsSelected properties.
As the navigation targets can be seen as a global collection, you can also hold it in a property on the ViewLocator, so that all Views and ViewModel can access this list, and setting the IsSelectedProperty is applicable to all Views. This global collection represent you navigation state.
And, BTW, the EventToCommand, the RelayCommand, and the Command attribute ony support one parameter. Furthermore, from experience I can advise you that it is not good practice (although you obviously can do it) to mix View objects, such as EventArgs, or elements and your ViewModel.
one solution is to put the menu items the ViewModel, and the View can bind this list.
then the View wouldn't need to send the menu items to the ViewModel

Xaml Dynamic Grid with Viewmodels

Hello i am trying to create a grid in xmal that is populated via a ViewModel.
The grid is a 5x5 grid, and my ViewModel contains a list of "MyObject". This object contains 2 int variables Row and Column witch indicates where in the grid the Object should be.
MyObject is also a view models, and the grid space should be filled with Data Template names MyTemplate with the MyObject as the DataContext.
Now I pretty new to xaml and view models, but how would the best way of doing the be?
Let's see...
Put an ObservableCollection into the VM, then...
Option 1:
...a simple Grid into V. Subscribe to it's CollectionChanged event from xaml.cs, add ContentControls for each Added object to grid, bind the ContentControl's Grid.Row and Grid.Column property to each object's properties, and set the DataContext to the object itself and ContentTemplate to Resources["MyTemplate"]. (Also, for any removed objects find the corresponding ContentControl and remove it.)
Pro: easy
Con: .cs, no designer support
Option 2:
...an ItemsControl into V. Set its ItemPanelTemplate to a Grid, and bind your ObservableCollection to its ItemsSource. Then set your Itemtemplate to MyTemplate. Now, the trick is to make the items put in the correct cell, based on your values. For that, use the ItemsControl's ItemContainerStyle property, and bind the container's Grid.Row and Column to those properties.
Pro: nice xaml and easily extended
Con: ItemContainerStyle is tricky in WPF and missing in Silverlight, so in case of the latter, you can forget it
Option 3:
...a custom panel which you write. Inherit it from Grid perhaps. Than if an element is put into it, check if it's DataContext implements an interface which contains your properties. Then use this as an ItemsPanel in Option 2...
Pro: it should work perfectly
Con: You have to write a new Panel for this...
Hopefully one of these 3 will be ok for you.