mvvm-light

How to have Multiple unique instances of ViewModel using MVVM Light?

烂漫一生 提交于 2019-12-03 16:17:37
I am fairly new to following the MVVM pattern. I am using MVVMLight. I am wondering how have multiple unique instances of the ViewModel with MVVM Light. For exmaple I have an application that can open n number of windows. Each uses the same Viewmodel. I am curious in MVVM whats the best practive to give them there own instance. If I follow the MVVM Light example the ViewModeLocator will have only a static instance which each window will end up using. Thanks in advance. You are not obliged to use ONLY the static view models in the view model locator. That approach only makes sense if your views

How to force Cleanup() for all my ViewModels

可紊 提交于 2019-12-03 15:04:52
My ViewModel instantiate resource that must be released when the program exits. this in all my ViewModels: public class MainViewModel : ViewModelBase { LocalServer Server { get; set; } Resource MyResorce { get; set; } public MainViewModel(LocalServer server) { this.Server = server; MyResource = new Resource(); } public override void Cleanup() { if (MyResource != null) MyResource.Close(); MyResource = null; base.Cleanup(); } } this in ViewModelLocator public class ViewModelLocator { public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default

How should I communicate between ViewModels?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 14:38:51
I am using MVVM Light and have used the packaged messenger system to communicate between view models, however I have hit a bit of a dilemma! Basically when a user clicks on a customer record the corresponding view is opened and with it the CustomerViewModel is instantiated. At this point the CustomerViewModel requires the selected customers ID from the previous view model ( ViewAllCustomersViewModel ) so that it can get selected customers info which the view binds to (still following?). So initially my thought was too send that ID in a message from the ViewAllCustomersViewModel (where the

CommandManager.InvalidateRequerySuggested does not cause a requery on CanExecute in MVVM-Light

£可爱£侵袭症+ 提交于 2019-12-03 13:07:32
I am using MVVM-Light RelayCommand private ICommand myRevertCmd; public ICommand Revert { get { if (myRevertCmd == null) { myRevertCmd = new RelayCommand(RevertExecute, CanRevertExecute); } return myRevertCmd; } } private void RevertExecute() { searchType = SearchType.Revert; SearchStart(); } private bool CanRevertExecute() { return isRevertEnabled; } I have some code that changes the value of isRevertEnabled but the linked button does not change. After some searching I found that you can use to force the re-evaluation of the button states // force the GUI to re-evaluate the state of the

MVVM-Light, firing events from a button inside a data grid column template

心已入冬 提交于 2019-12-03 12:37:01
问题 MVVM light has been a pleasure to learn, but here I am stuck. The problem is event firing. In the code below, one button the works and fires events. The other button doesnt. No binding errors are reported in the output. Is there anything obvious I am missing? <Grid x:Name="LayoutRoot">... <StackPanel> <Button Content="THIS BUTTON WORKS"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <Command:EventToCommand Command="{Binding DataContext.HandleAddQuestionActionCommand, ElementName

What is a good way to bubble up INotifyPropertyChanged events through ViewModel properties with MVVM?

一笑奈何 提交于 2019-12-03 10:15:10
I'm trying to figure out the best way to bubble up PropertyChanged events from nested Properties in my ModelView. Say I have my ModelView PersonModelView which has a Property PersonModelView.Address . Address in turn has a property City . When I bind to City in my view, I would do something like {Binding Address.City} . My problem is that even if Address implements INotifyPropertyChanged , the binding will not get updated because it is handling PropertyChanged on the PersonModelView , not Address . I think I have two options: (1) change the source of the binding (or change the DataContext ) to

How to receive DialogResult using mvvm-light Messenger

天大地大妈咪最大 提交于 2019-12-03 09:51:54
问题 I'm trying to use the mvvm-light messenger capability to open a custom confirm password dialog in my view, triggered by a command in my viewmodel. I think I understand the usage of Messenger.Default.Register and Messenger.Default.Send . But how do I get the dialog results back in my viewmodel? To me the sending seems to be a one way street... Could someone help a beginner with a small C#/WPF code sample? Thanks for any help 回答1: IMHO it is better to use the NotificationMessageAction<T> as it

How do you access the MainViewModel in ViewModelLocator from code behind?

最后都变了- 提交于 2019-12-03 09:16:42
Building a WP7 app using MVVM light for my view models. I'm using the ViewModelLocator that gets added when you add the library through NuGet. Works great but now I need to get access to a ViewModel from code. In my code the user clicks a button and I need to search the MainViewModel (which contains several view models) and find one based on the criteria the user entered. Normally I would just response to the Click event of the button but I don't have an instance variable of the ViewModelLocator class to get a hold of the MainViewModel to perform the search. With the default template (non

MVVM Light - User Controls as Views

怎甘沉沦 提交于 2019-12-03 09:06:15
I have decided to use the MVVM Light library to help design a UI. After tons of research and trial and error, I have yet to find the answers I am looking for. I've googled and read each StackOverflow question I can find, however, my problem seems to be unique on SO. I wish to design a UI with a single window and populate it with different Views/UserControls. I do NOT want a shared navigation bar among the UserControls nor do I want multiple windows to pop-up. Each View/UserControl should bind to its own ViewModel, while the MainWindow will bind to a MainViewModel. Example scenario - MainWindow

Creating a dynamic checkboxes against the list of items using mvvm light wpf?

放肆的年华 提交于 2019-12-03 08:45:38
I have the following scenario: I have one window say MainWindow where I am displaying the list of activities as per the specific user from the database. There is a one button present on the window. By clicking on that button a new window is getting opened having all the list of activities from the master table. Now I want add a chechbox on the second window against each item dynamically so that user can select/deselect the activities. Those selected/deselected values should save in the database and Parent/MainWindow should refreshed after clicking on the done button and changes should reflect