WPF mainwindow controls binding property changed from another window - wpf

I`m new to WPF and databinding so I stumbled on something.
I have created an app with vb.net where my MainWindow contains controls and a frame in which I load a page.
On the page I have a listview where the data is loaded from the database.
Now, I have created the Viewmodel and set up the properties to bind to the controls on the Mainwindow so I could set IsEnabled property to True or False through the INotifyPropertyChanged.
If I change the properties from the mainwindow code, is working. What I want to be able to do is when I click on an item on the page-listview to change the properties in the viewmodel and reflect it back to the UI of the Mainwindow.
I`v searched and could not find anything to help me out.
Does anyone have a suggestion on how can I achieve this or some directions to the right place where to find useful information or at least what needs to be used so I can research more of it?
Cheers,

If the "Child" viewmodel has a reference to the "Parent" viewmodel it can update the properties on the parent and the INotifyPropertyChanged system should update the UI for you.
I'm not entirely sure this is what you are asking about though, your question is a little unclear.

Related

Animate a Helix Toolkit control drawn by a usercontrol

I am trying to achieve something very similar to this:
WPF with Helix toolkit, animate with code-behind?
However, my scenarios is slightly different. I am trying to animate a tube (curve) defined by a path (TubePath) which undergoes deformation as a function of time (animation) defined by a mathematical function (calculated numerically).
My design/plan is to use a usercontrol (containing the Helix Tube control) to draw each frame of the tube defined by a path. I plan to make the TubePath property in my usercontrol which is an oservablecollection a dependency property. I plan to control the animation from my ViewModel (or View) by binding the CurrentPath property of the ViewModel to the usercobtrol TubePath property. I plan to drive my ViewModel from INotifyPropertyChanged. I will a have List of Paths (PathList) stored in advance of starting the animation in my Model. At each (so called) time or animation step, I will copy the corresponding path from the PathList to the CurrentPath in the VieModel. I am hoping that change in the the CurrentPath collection in the ViewModel will activate the binding which will force the user control to update the tube drawn by the usercontrol.
These are my questions:
(1) I have read that not all changes to an ObservableCollection fires propertychanged events. Will overwriting or refereshing the whole CurrentPath collection in the ViewModel update the TubePath in the UserControl which will force redrawing of the tune? Do I have to do anything special to achieve this kind of binding.
(2) I am new to WPF and animations with WPF. My research indicated that I could do the animation from my ViewModel in several ways: using the Dispatcher.Invoke() like in the example given above, or I could use the RenderingEventManager.AddListener() (and RenderingEventManager.RemoveListener() to stop animation) provided by the Helix Toolkit like it is done in example here:
https://github.com/helix-toolkit/helix-toolkit/tree/develop/Source/Examples/WPF/ExampleBrowser/Examples/Wind
or I could loop using DispatcherTimer() as done here:
Binding on DependencyProperty of custom User Control not updating on change
Any suggestions as to the best method as well as my concept of driving animation from the ViewModel would be greatly appreciated.

WPF Prism Unity Container. Setting keyboard focus on usercontrol or usercontrol grid parent

I'm working on a project that utilizes WPF, using the Prism library and Unity Container.
I have a mainwindow, which contains a mainviewmodel control which in turn is populated by a collection of user controls.
My goal is to set keyboard focus to the mainviewmodel where I have an EventTrigger InvokeCommandAction which reacts to keyeventsargs...
Currently the only way the command is fired if I use a textbox within the control (or child controls). My goal is to have the mainviewmodel control or grid get and preserve keyboard focus.
Any tips would be appreciated!
//Nathan
Either not understanding your question correctly or you should review the basic concepts of MVVM in a WPF implementation.
The View is the WPF controls.
WPF Window/UserControl files contain WPF markup which is the View.
Controls in a view leverage DataBindings to the DataContext property of either the control itself or the parent containing control (which it will inherit).
DataContext property is set to an instance of an object that is the ViewModel. It contains properties to hold values and commands to execute actions.
So conceptually there is no "mainviewmodel control", there is a MainView which contains controls and may in this case have its DataContext property set to an instance o MainViewModel. (hence my confusion)
Finally, while it is possible and some might even recommend writing UI rules/logic in a view model I haven't found much benefit in it.
You are much better off putting UI logic in the XAML or in the MinView code behind. You can still access the MainViewModel in the code behind by casting the MainView.DataContext property as a MainViewModel.
So for example:
MainView.KeyDown event can be wired up to call MainViewModel.CommandX.Execute();

MVVM - Menu View with Preview View (Shared ViewModel) approach

I've just started my journey with MVVM and WPF and I need an advice to solve my problem or rather guide me towards right solution.
Link to image: https://s12.postimg.org/md0h6fv3x/Simple_App.png
Description (what I want to achieve):
View A is a menu (UserControl) with ListBox and DataContext: V.ObservableCollection
View B is a preview to selected item from View A ListBox
View A & B are loaded from MainWindow
What is simpliest and cleanest approach to achieve that?
I was thinking about sharing the same ViewModel with View A&B.
Create a SelectedItem property in ViewModel and then put it MainWindow's resources and bind datacontext to View A & B
Is it valid approach? Will that VM be updated properly in both views if it get changed?
Somehow bind View's A listbox SelectedItem to View B preview mode but don't know how.
Use only one ViewModel.
Eg; Pseudo code :
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<Employee>{get;}
/* provide change notification in ChosenItem */
public Employee ChosenItem{get;set;}
}
Use this ViewModel as DataContext for both Views.

List<> Binding and button click using mvvm light

I am trying to use MVVM light to achieve something like this. I have the following scenario:
In my Model--I have set the properties like ActivityName,Image and there is a class constructor whose is accepting 2 parameters like name and image.
Im my DataAccess--I have set the database connection and implement the required method who will fetch the data from DB and I am storing in the List and returning the list to ViewModel.
In my ViewModel--I have created the list property who will return the list by calling the GetActivities() method that I have defined in the DataAccess.
Now my problem is I am not getting how to bind it in the View so that by clicking on a button it will display the list of activities with image. By clicking on some button a new window should open with the desired result. How to bind the above list and implement the button functionality using MVVM light.
Kindly help?
Thanks
First of all, use an ObservableCollection instead of a List as it would notify the view when property or collection changes.
Then set the DataContext of your view to the viewmodel. If you use MVVMLight View Class, then the DataContext would be automatically set. You've to just give the ViewModel Name there.
Then set the ItemsSource of the DataGrid like this <dg:DataGrid ItemsSource="{Binding YourListInViewModel}"/>
For handling click event you can use the Event-To-Command behaviour and write your logics in the Button's corresponding Command handler.
Bind to DataContext of the control

MVVM Update binding issue

I have user control to which I bind a viewmodel, this implements the INotifyPropertyChanged, thru the datacontext, this has a property that is a IList that I bind to the itemsdatasource of a grid, then on the code, in another class I add some values to the list, but the UI doesn't reflect this change although in debug I can see that the datagrid has this items added but they don't appear in the UI, can anybody help me, I can't see what is the problem.
ObservableCollection<T> is your friend. It implements INotifyCollectionChanged, saving you the trouble.

Resources