WPF UserControl Expose Custom Property - wpf

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

Related

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.

How do I bind wpf with specific datacontext from different classes?

Hi guys I am very new to WPF. I have two datacontexts in two different classes which are being binded by the elements in the View producing datatriggers, and one or the other wouldn't work as they cannot bind both datacontexts together. How do I bind xaml from two different classes using datacontext? Is there any alternative way could make it easier?
Class A
public Window1()
{
InitializeComponent();
Appointments = new Appointments();
DataContext = Appointments;
}
Class B
private void FilterAppointments()
{
this.DataContext = this;
...
Firstly, you should never use DataContext = this; in any UserControl in a serious WPF Application. Secondly, you should look up the MVVM design pattern, which provides the idea of a view model for each view. Your Window or UserControl are the 'Views' and your view models are simply classes that contain all of the data properties that you need to display in your view.
Therefore, you should declare a view model class (that implements the INotifyPropertyChanged interface) and put whatever you wanted to data bind into that. Finally, you should set that object as the DataContext property value. In that way, you'll have access to all the data that you need.
Looking again at your question, it just occurred to me that you may have set the DataContext to this so that you could use properties that you declared in your Window or UserControl. If this is the case, then you should not set the DataContext to this, instead using a RelativeSource Binding to access the properties. That would free up the actual DataContext to be set however you like. Try this Binding within the Window or UserControl:
<TextBlock Text="{Binding PropertyName, RelativeSource={RelativeSource
AncestorType={x:Type YourPrefix:YourWindowOrControl}}}" />

Sharing ViewModel's Property

Goal
To have a DataGrid in my MainWindow.xaml contain whatever I add in another Window.xaml's ViewModel Property.
I am looking how to display the exact information in my MainWindow's DataGrid.
Background Information
MainWindow.xaml has a DataContext to ViewModel InspectionViewModel.vb
Window.xaml has a DataContext to ViewModel DamagedItemViewModel.vb
My ViewModel contains an ObservableCollection of my Model: DamagedItemModel.vb
I want to be able to gather whatever is on my ObservableCollection and put it into my MainWindow's DataGrid's ItemSource. Is this possible?
In my DamagedItemViewModel I shared the public property so that I could access it from my InspectionViewModel.
InspectionViewModel Modifications
I made my ViewModel inherit INotifyPropertyChanged and added the same ObservableCollection property that my DamagedItemViewModel contained. Once the user was finished with the Window.xaml, I just assign the DamagedItemViewModel.DamagedItems to my InspectionViewModel.DamagedItems.
DamagedItems = DamagedItemViewModel.DamagedItems

Retrieves value from Wpf UserControlLibrary to WpfForm

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.

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