Caliburn.Micro - pass readonly properties to view model - model-view-controller

in my Windows Phone 8.1 application I'm working with images (cropping, applying filters etc.) thus I need controls' readonly values such as ScrollViewer.HorizontalOffset or Canvas.ActualWidth in my view model.
How can I pass such values to the view model with Caliburn.Micro? When they are readonly, I can't access them in the xaml.
Thanks

this is example with Loaded event, you can use better event for your use
micro:Message.Attach=[Event Loaded] = [Action LoadCanvasProperties($this.ActualWidth, $this.ActualHeight)]"

Related

Binding to value in parent page from in usercontrol in listview datatemplate in Windows Phone 7

I have a ViewCharacter page that has a View Model (CharacterViewModel) as its DataContext, in that CharacterViewModel I have an ObservableCollection of WeaponViewModel that a ListBox is using as an ItemSource. The ListBox's DataTemplate contains a UserControl that is designed for the WeaponViewModels in the parent page's View Model's ObservableCollection. I need to bind a CommandParameter of a button in the UserControl to a property on the Parent page's view model (CharacterViewModel). I've tried using {Binding DataContext.TargetProperty, RelativeSource={RelativeSource TemplatedParent}} with no success and am at a loss of what to do without outright breaking all MVVM patterns.
Windows Phone 7 lacks a relative source binding. I have created a Silverlight implementation here:
http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/
But for WP7, where performance is critical, I would avoid using it!
I would instead recommend making the relationship between CharacterViewModel and WeaponViewModel bi-directional. In other words, add a Parent property to WeaponViewModel which will be a reference to the owning CharacterViewModel. You can then bind to properties on CharacterViewModel via this property.

Passing user control properties from controller to view in MVC3

I have created a layout page which has some regularly used user controls (like buttons, text box etc). But the visibility of these controls are determined by the view which is defined from this layout page. What is the best way of setting these values of the view in MVC3? can I pass the property values from the controller to the view?
Any help will be appreciated.
Thanks,
--Kuppa
ViewBag should be able to help you out http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx

Setting DataContext in UserControl from ViewModel

I have a View that contains a Listbox. I'll call it CityListingPage.xaml. This list page has a CityListingViewModel, that is binded with the View like somewhat like this:
DataContext="{Binding CityListing, Source={StaticResource Locator}}"
This works nicely. Now I what to change my page to a Pivot Control, where the Pivot Items, would be instances of CityListingViewModel, but obviously with different constructor data (ie. Country)
I extracted the ListBox into a UserControl. Now I'm struggling how to make this work so that for each list I get a new instance of the CityListingViewModel.
I tried creating in the ViewModelLocator a collection of CityListingViewModels but how do I pass the a CityListingViewModel instance to the UserControls DataContext?
Perhaps there is a different, better way of doing this?
Without seeing your code, I'm going to do a little guessing, but I think you can do it directly via data binding. Since each pivot item is getting an instance of CityListingViewModel, you can just pass that binding along to the UserControl:
If you post a little more code showing what you're trying to do, we might be able to be of more help.
The following are two answers for using a collection to create panorama pages. But I am quite sure that the approach can be adapted to pivot pages:
Dynamically add eventtocommand actions to a listbox
Static and dynamic panorama items in a panorama wp7 mvvm
The second post should be more relevant.
If you are thinking of partitioning the same data over multiple views on a pivot page then I would suggest NOT using several view models, especially if it is the same datasource you are using for all the data.
Simply have a parameter which each view would bind to and use Linq to control what data is visible to that parameter.
So you will have the variable which will contain all the data to be displayed and one parameter per view querying that data.

Using PhoneApplicationPage as a nested view container

In MVVM Light toolkit for Windows Phone, whenever I am to add a new MvvmLightView (WP) item, I end up with the template creating a PhoneApplicationPage for me.
What about cases, when I want to create a nested view, for example in case of a ListBox ItemTemplate view.
Before MVVM Light, every time I needed a view to separate markup to, I would have created a standard UserControl and that worked fine.
Should I only use MvvmLightView whenever creating a navigable pages?
The item template is a guidance, that produces some code for you. It is not, nor does it intend to be, the single way of creating views or sub views.
In the case you are mentioning, it is usual to create a user control hat is backed on its own view model. This sub-view view model is then included as a property in your main view model. To pass it to your sub-view (e.g. a user control) you bind this property to the user control's DataContext.
<ext:MyUserControl DataContext="{Binding MySubViewProperty}"/>
However, you do not need a separate view model, in some cases it is more appropriate to share the main view model. In this case you do not need to do the above binding, as it is do one implicitly. Also, when you are using the user control within an DataTemplate the templates DataContext is passed to the user control implicitly and you do not need the binding. In general you only need to set the data context when you want to bind to a property of the current context, or to another context.
MVVM is about freedom and MVVM Light about supporting the developer in using this freedom. All guidance are best practises and provide usually the easiest an/or most consistant way, but nothing stops you going down another route for a good reason. Especially the templates are just shortcuts that provide for one problem, but not for the general (meaning every) case.

Sharing viewmodel to multiple views using Caliburn.Micro in WP7

I am currently working on a project which requires multiple views of the same viewmodel. Let me describe this way:
ViewModel: CustomerDetailsViewModel.cs (inherited from Screen class)
View: CustomerDetails.cs (this view has CustomerDetailsViewModel as datacontext and this set automatically by Caliburn.Micro)
View: CustomerInfo.cs (now this is the view where I want to share CustomerDetailsViewModel, which could have some data already modifed via CustomerDetails view)
I am currently using NavigationService to navigate to CustomerInfo view. Is there any way to pass the reference of current viewmodel to the view which user is navigating to in caliburn.micro?
Thanks in advance
idev
Use the attached property cal:Bind.Model="{Binding}" to bind the view to the view model.
See http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions "View First" section.
Alternatively you can also look into the cal:View.Context="MyContext" attached property as described here: http://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition "Multiple Views over the Same ViewModel" section.
Add a property or two to your App.xaml.cs. What ever you put in here will persist throughout the lifetime of the application (keep in mind that tombstoning will cause this property's value to be lost though). If you want to pass a ViewModel then setyour associated property in App.xaml.cs to the view model and then when the new page loads have it read from that same property.

Resources