Setting a data binding on a UI control created programmatically in a Windows 8 C++/Xaml app - visual-studio

As per this page and several other references, the way to create a data binding with Xaml/C++ in Windows 8 is to do the following:
In C++:
Create a bindable class,
Set the DataContext property to an instance of that class.
In Xaml:
<Object AnyProperty="{Binding Path=AnotherProperty, Mode=TwoWay}" />
How would I achieve that binding if I constructed Object through C++?

Would this page be of any help to you?
How to: Create a Binding in Code
The example on the page seems to use a SetBinding instance method on a textblock to bind a property to the text property of the textblock. Maybe there is a similar method in C++?

Related

Binding a command and a CommandParameter in the same view

I have an Entry and a Button. I want the command "CallWebServiceCommand" to be called when I press the button. The call to that command needs to include the url of the web service as a CommandParameter. The BindingContext is set to the ViewModel of the page.
The CommandParameter property of the button needs to reference the Text property of the entry. In WPF, I could do something like this:
<Button Text="Call web service" Command="{Binding CallWebServiceCommand}" CommandParameter="{Binding ElementName=url, Path=Text}" />
I know that it's not possible to have multiple binding contexts per view, but what would be a good workaround for this particular situation?
This is a bit of a hack, but it's worked for us in the past:
Use the ViewModel as a "relay" for the view. To do this, create a String property on your ViewModel that the text field binds its Text property to, and bind the CommandParameter of the button to this property. If you raise the PropertyChanged event for this "parameter" property, the command will supply the updated value to the method specified as the command's Action. It's certainly non-ideal, but it does work as a poor man's replacement for RelativeSource binding.

Caliburn.Micro - pass readonly properties to view model

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)]"

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.

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.

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