mvvm-light

Silverlight Constructor Injection into View Model + Design Mode

99封情书 提交于 2019-12-18 11:12:19
问题 Im trying to get to grips with writing testable ViewModels in Silverlight 4. Im currently using MVVM light. Im using AutoFac and the IoCContainer is doing its job fine. However to inject into the constructor of ViewModels, which are bound to Views I have this constructor chaining: public UserViewModel() : this(IoCContainer.Resolve<IUserServiceAsync>()) { } public UserViewModel(IUserServiceAsync userService) { if (this.IsInDesignMode) return; _userService = userService; } Which doesn't feel

WP7: Why does a ListBox.ItemsPanel break my ElementName data binding?

巧了我就是萌 提交于 2019-12-18 07:03:16
问题 I have a Windows Phone 7 ListBox that binds to a list of integers. I am using the default MVVM Light template, so there is a ViewModel class that contains data and a simple RelayCommand . Here is the ListBox: <ListBox ItemsSource="{Binding MyData}"> <ListBox.ItemTemplate> <DataTemplate> <Button Content="{Binding}"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <cmd:EventToCommand Command="{Binding ElementName=ContentGrid, Path=DataContext.TestCommand}" CommandParameter="{Binding

CollectionChanged sample

家住魔仙堡 提交于 2019-12-18 06:17:12
问题 Can someone point to an example where CollectionChanged is implemented. I am using wpf mvvm light. I tried to google, didn't find anything good enough. 回答1: public ObservableCollection<string> Names { get; set; } public ViewModel() { names = new ObservableCollection<string>(); Names.CollectionChanged += this.OnCollectionChanged; } void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { //Get the sender observable collection ObservableCollection<string> obsSender = sender

Passing event args and sender to the RelayCommand

泄露秘密 提交于 2019-12-18 05:46:11
问题 How do you get event sender when using RelayCommand? 回答1: This is one of those pain-in-the-%¤# answers where I don't actually answer your question, but instead lecture you about what you should be doing differently. So, sorry about that. Here goes: If you find yourself in a position where you need to get at the sender object in your viewmodel, then you should probably do something different. By referencing, say, a Button or a ListBox in your viewmodel you have made that viewmodel aware of UI

How to reference a generic type in the DataType attribute of a DataTemplate?

↘锁芯ラ 提交于 2019-12-18 05:43:53
问题 I have a ViewModel defined like this: public class LocationTreeViewModel<TTree> : ObservableCollection<TTree>, INotifyPropertyChanged where TTree : TreeBase<TTree> I want to reference it in the DataType attribute of a DataTemplate in XAML. How can I do that? 回答1: No, you cannot express a generics type in XAML. You will have to create a concrete type that extends your generic one ... public class FooLocationTreeViewModel : LocationTreeViewModel<Foo> { } 回答2: In XAML 2006 this is not supported.

MVVM Windows Phone 8 - adding a collection of pushpins to a map

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 03:01:44
问题 Here is the XAML code: <maps:Map x:Name="NearbyMap" Center="{Binding MapCenter, Mode=TwoWay}" ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}" > <maptk:MapExtensions.Children> <maptk:MapItemsControl Name="StoresMapItemsControl" ItemsSource="{Binding Treks}"> <maptk:MapItemsControl.ItemTemplate> <DataTemplate> <maptk:Pushpin x:Name="RouteDirectionsPushPin" GeoCoordinate="{Binding Location}" Visibility="Visible" Content="test"/> </DataTemplate> </maptk:MapItemsControl.ItemTemplate> </maptk

Navigating to a new page from the View Model in Windows Phone 8.1 universal app

一笑奈何 提交于 2019-12-18 01:21:13
问题 I am working on a windows phone 8.1 universal app and want to find the best way of handling page navigations without having large amounts of logic in the code behind. I want to keep the code behind in my View as uncluttered as possible. What is the accepted MVVM way of navigating to a new page in response to a button click? I currently have to send a RelayComnmand message from the ViewModel to the view with the details of the page to navigate to. This means that the code behind has to be

How to navigate through windows with MVVM Light for WPF?

馋奶兔 提交于 2019-12-17 22:08:49
问题 I've just started a new project in which the presentation layer will be done by WPF and MVVM Light by GalaSoft. I need a lot of views and it's not clear to me how to manage navigation through windows. First of all, the templates offered in MVVM Light for creating a new "WPF MVVM View" create a new Window that is not possible to use for navigation by frame (I mean, by putting a frame in mainView and changing the source path to navigate). Do I simply have to change Window to Page for all the

Simple example of DispatcherHelper

自作多情 提交于 2019-12-17 19:46:16
问题 I'm trying to figure out how can I use DispatcherHelperftom MVVM light toolkit in SL, but I can't find any example. From home page of this framework I know that DispatcherHelper class, a lightweight class helping you to create multithreaded applications. But I don't know how to use it. How and for what I can use it? 回答1: You only need the DispatcherHelper when yo want to make changes to components on your UI thread, from code that runs on a different thread. E.g. in an Silverlight application

MVVM light - how to access property in other view model

陌路散爱 提交于 2019-12-17 06:11:30
问题 I'm using mvvm light to build a Silverlight application. Is there a code snippet that shows how to access a view model's property or command from within another view model or user control's code behind? I guess it's simple, but I somehow missed something. Ueli 回答1: You could use the Messenger to do this: Send the user in the UserViewModel: Messenger.Send<User>(userInstance); would just send the user to anyone interested. And register a recipient in your CardViewModel: Messenger.Register<User>