Retrieves value from Wpf UserControlLibrary to WpfForm - wpf

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.

Related

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();

wpf checkbox binding with user controls

I am working on wpf mvvm pattern. I have different user controls. Based on the checkbox selection, I want them to be loaded in the main screen (that is also a user control). I have one HomeViewModel class which I have been using to bind the user controls of my project. Can you help me with a suitable way?
You should have different ViewModels for each kinds of UserControl.
Create different DataTemplates for each ViewModel Types
Put a ContentControl with binding a property of HomeViewModel -
Value of property will be an instance of ViewModel ( UserControl's) and is set by toggling CheckBoxes.

How to detect changes between two usercontrols?

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.

WPF UserControl Expose Custom Property

I am developing an application following the MVVM model and writing my code in VB.
I have created my own UserControl, let's call it 'Arrow', which has a property in its ViewModel called 'Angle'. In my main window I have a ListBox which is bound to a collection and each ListItem will be an Arrow.
How can I bind an angle value in the ListBox's DataSource to the Angle property of each instance of my UserControl please?
From my research so far it looks as if I need to create a DependencyProperty for Angle. If this is the case do I create this in my ViewModel, in the code-behind the XAML or elsewhere?
I'm sure this is simple when you know how!
The DependencyProperty would be defined in the UserControl (the code-behind). You'd then bind that property to the property in the ViewModel

How can I easily expose bindable properties of nested controls from a custom UserControl?

My first question here on the Stack. Forgive me for the bad explanation in advance.
I am working on my first MVVM application (Silverlight). I have a custom user control that contains a ListBox to show navigation items. This control is placed in my main xaml page. I don't know if I need to create a composite view model (my main page view model) with a view model especially for the custom control in it or if there is some way to elevate the ListBox properties that I need to bind to.
Through XAML I don't know how to bind, let's say, the ItemsSource property of the ListBox inside the custom control to my main page viewmodel. Basically, I'm at the point that I am questioning my design decision for trying to bind the custom control through my main page view model.
What I have done so far is create dependency properties for the custom control and try to tunnel those dependency properties down to the ListBox properties. I've achieved success with this method for ItemsSource but am having issues with SelectedItem.
Even if I do get SelectedItem to work, it still feels Wrong. Thanks for any advice in advance.
The UserControl should inherit the DataContext from its parent control, unless you are setting it directly. You can then bind to the properties on your view model from your UserControl.
If you would like to create a ViewModel specifically for the UserControl, you can also do that. You would then expose it as a property on your main ViewModel, and bind to it in the MainPage. Example:
public class MainViewModel
{
public ChildViewModel ChildInfo { get; private set; }
}
And then in the view:
<Grid>
...
<lcl:ChildView DataContext="{Binding ChildInfo}" />
...
</Grid>
Your ChildViewModel would then contain properties like SelectedItem to bind your ListBox to.

Resources