I have a main window that contains two usercontrols.
The first usercontrol have a tabcontrol.How can I notify the second usercontrol when a tabitem is selected in the first usercontrol.
If you're using MVVM approach, you'll probably have bound SelectedIndex of your TabControl to a ViewModel property. In that case your second usercontrol will bind to the same (or some other) property of the ViewModel and will be notified through standard notification mechanisms (such as INotifyPropertyChanged or DependencyProperty etc.).
In case, you are not using ViewModels and coding directly behind your Window, you can listen to SelectionChanged event and update your second usercontrol therein.
Related
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();
I've searched a lot for an answer for this question, but couldn't quite find an answer.
I have my main Window and 4 different UserControls. The Window is constructed from all 4 UserControls.
I have a class which is the Window's ViewModel.
I wish to set the DataContext of the Window and all 4 UserControls as the Window's ViewModel.
The problem is that writing the class as the control's DataContext creates a new instance of the class, hence, I can't use the containers I'm filling.
Any way of doing that?
DataContext gets inherited in WPF.
If you don't set a DataContext for the UserControl, they will automatically inherit their parent's DataContext (the Window), and should use your ViewModel directly.
I have 2 datagrids in my UI which lists vehicles. Both DataGrids are exactly the same except they maintain 2 different ObservableCollections.
I created a VehicleListViewModel which contains an ObservableCollection property and different ICommand properties. So I have DataGrid1's DataContext set to the first instance of VehicleListViewModel (ViewModel1) and the 2nd DataGrid's DataContenxt to the second instance (ViewModel2).
Now, I need to implement the PreviewDrop for both datagrids. I want to have the code for this inside the ViewModel as well and not in the code-behind. However, setting PreviewDrop="xxxxxx" only allows the event handler to be defined within the code-behind. Does anyone know a way to do this?
EventToCommand Behavior:
EventToCommand
Place this on your datagrid and bind it to a command property in your viewmodel.
I have a wpf wpplication with a number of user controls in it. One of these controls has a property called ButtonsEnabled. This is a bool DependencyProperty in the user control. The property is bound to the IsEnabled property of a couple of buttons on that control.
This user control is used in the MainWindow. The MainWindow has a couple of view model objects in it called EocMonitor and ComMonitor. These both descend from an abstract base class that implements INotifyPropertyChanged. The ButtonsEnabled property on the UserControl is bound to the Status property using a multibinding and a class that implements IMultiConverter that I wrote.
The problem is that even though the PropertyChanged event is being raised when the Status property changes, the IMultiConverter is not being called after it is initially called, so the value of the ButtonsEnabled property is not changing. As a result, the buttons are not enabling.
What do I need to do to make this work?
I did an end-run around this problem as I am running out of time before we reach our code-freeze for this release. What I did was I added a ButtonsEnabled DepdendencyProperty to the MainWindow class and bound it to the user control's ButtonsEnabled property. I then added a PropertyChanged event handler in the MainWindow and and registered it with the DbMonitor and ComMonitor objects when they were created. I then wrote code in the PropertyChanged event handler to set the MainWindow's ButtonsEnabled properly.
Everything works and I'll worry about making the other approach work at some later time. Maybe.
In my WPF application, I have a usercontrol library. In this library, I have a listview control along with textbox and button controls.
I have placed this usercontrol in a WPF window form. How can I retrieve the listviewitems of my usercontrol in this WPF window form?
You ask a very general question. There are dozens of ways to do that, depends on your needs. For example: when you place you UserControl within the Window - you should name it (e.g. . Then, within the UserControl, you can have a public function, or a public getter, which will return the items (it is the 'Items' property of the ListView).
Another way is binding. If you have a ViewModel, or any other class as the DataContext, you can bind the ItemsSource of the ListView to a collection on your ViewModel. Unless you change the DataContext of the UserControl - it will have the same DataContext as the main Window.