ItemsControl Binding to a DataModel.ObservableCollection - silverlight

I have a view (MainPage.xaml) which is bound to a ViewModel.
In the ViewModel I have a DataModel property (note that both viewmodel & datamodel implements INotifyPropertyChanged, or what that interface is called).
In my view i have defined an ItemsControl whose ItemsSource is bound to the said property. This property (DataModel) has an ObservableCollection (which i know it populated with valid data).
The xaml snippet looks like this:
<ItemsControl ItemsSource="{Binding Path=CurrentDataModel.Items}">
Note that i am not showing the rest of the xaml. All it shows is the ItemsTemplate which is just a TextBlock.
The issue is that nothing is being drawn for this ItemsControl, even though i can clearly see (while debugging) that the collection has good data.
Is this "Path=..." binding not possible for an ItemsControl's ItemsSource?

The Path syntax looks correct. The most likely cause of failure is that either the DataContext is not set to your ViewModel or that the property path you specified is incorrect.
In your question you state that the ViewModel has a property named DataModel but in the xaml snippet you have CurrentDataModel.

Related

Binding a control when the View is bound to a ViewModel?

Here is my situation. I have a View and a ViewModel. The view's DataContext is set to the ViewModel. Due to the use of a 3rd party control, I am forced to put some code in the code-behind. In the code-behind I create an object called StraightConnectorTool.
In my View, I need to bind to this object. If the View's DataContext is set in the code-behind:
DataContext = this;
The following binding works fine.
<BarItemToolBehavior ActiveTool="{Binding ActiveTool, ElementName=diagram, Mode=TwoWay}"
Tool="{Binding StraightConnectorTool}"/>
Where diagram is the name of the 3rd party control on the View and ActiveTool is one of it's properties.
However, if the View's DataContext is set to the ViewModel, the binding doesn't work. I'm stuck trying to figure out how to bind to the view when it's DataContext is set to the ViewModel. Any ideas?
It's not good practice, but you can bind the BarItemToolBehavior's DataContext to the view. Either by name in the code behind or in XAML using RelativeSource FindAncestor to find the view. A better solution would be to move that object to the VM where it belongs.

trying to bind datagrid item source to a property in another class

I have a WPF app with a MainWindow. The MainWindow consists of several CLR properties of type ObservableCollection. The MainWindow has a datagrid, whose ItemsSource property is bound to one of the observable collections (works fine). Next, I have a dialog. Its purpose is to display one of the observable collections from the main window in a datagrid. The dialog gets instantiated in the MainWindow. Initially I was passing the ObservableCollection to the dialog's constructor, and copying it into the dialog's CLR property. Then I would set the DataContext of the dialog to itself, and bind the ItemsSource property in the datagrid to the name of the CLR property. This worked fine.
Is there a better way to do this instead of passing the observable collection through the constructor? I tried setting the ItemsSource property of the Datagrid in the dialog to the observable collection in the MainWindow by using the GUI editor, which generated a binding using RelativeAncestor, but the data did not show. The problem is I have a bunch of dialogs that are meant to display data from the MainWindow, and I feel like there should be a simpler solution rather than passing everything to dialog's constructor. Also, would the dialogs be considered SubViews? The main window is a view.
Let's say your Dialog control is named DialogControl and has a DependencyProperty named Items defined in its code behind. In the XAML, I would bind this property to the DataGrid like this:
<DataGrid ItemsSource="{Binding Items, RelativeSource={RelativeSource Mode=
FindAncestor, AncestorType={x:Type DialogControl}}" />
This RelativeSource binding will go off and search through the properties of your DialogControl class and find the Items property. Note: Do NOT set the DataContext of the UserControl to itself.
Now in your MainWindow.xaml.cs file where you instantiate your DialogControl, you can set the Items property:
DialogControl dialogControl = new DialogControl();
dialogControl.Items = someCollection;
dialogControl.Show();
UPDATE >>>
Oh I see what you're after now... you want to bind from your UserControl to the actual collection in the MainWindow.xaml.cs file. You can still follow my advice, but instead of having the DependencyProperty in your DialogControl, you need to have it in your MainWindow.xaml.cs file. In that case, your binding in the UserControl would be:
<DataGrid ItemsSource="{Binding Items, RelativeSource={RelativeSource Mode=
FindAncestor, AncestorType={x:Type MainWindow}}" />
For this to work, the Items property must be a DependencyProperty.

Binding with parent DataContext

I'm trying to bind combobox editor in a PropertyGrid to a list.
<dxprg:PropertyGridControl SelectedObject="{Binding SelectedEmployee}">
<dxprg:PropertyDefinition Path="EmployeeCountryID">
<dxprg:PropertyDefinition.EditSettings>
<dxe:ComboBoxEditSettings
ItemsSource="{Binding Path=DataContext.Countries, ElementName=rootWindow}"
ValueMember="CountryId" DisplayMember="CountryName" />
</dxprg:PropertyDefinition.EditSettings>
</dxprg:PropertyDefinition>
</dxprg:PropertyGridControl>
This example is from a third-party control but the problem may be just general.
The "rootWindow" DataContext has been set to a ViewModel which holds a property List(of Country) that I want have as ItemsSource in a Combobox.
I was trying to access that list by setting the Combobox ItemsSource to the rootWindow.DataContext.Countries property but I don't get any data.
Tried also all those RelativeSource FindAncestor bindings but no data appeared either.
Why can't I bind through a DataContext of a given element like this?
This became solved. The problem was not with the binding at all but realated to how I defined the third-party control: Instead of EditSettings I should have defined CellTemplate -> DataTemplate.

Binding lost when changing DataContext

I have a combobox in my MainWindow window.
<ComboBox ItemsSource="{Binding GroupMethods}" SelectedItem="{Binding SelectedGroupMethod}"/>
I bind it to List which is in my base ViewModel class and it works at beginning.
I want that combobox to be everywhere in my application.
Now i switch DataContext of MainWindow to another viewModel which inherits from ViewModelBase (where the GroupMethods property is), i update that property in some point and see no results. Binding simply didn't recognise that DataContext has changed and still "looks" at previously saved property.
Fireing PropertyChanged event didn't solve the problem.
How do I make it work?

How do i access a combobox text in a usercontrol from the wpf forms viewmodel?

I have a UserControl with 4 combobox bound to collections in viewmodel for that usercontrol.
I have used this control in a wpf form. This wpf form has its own viewmodel.
How do i access the text from the 4 comboboxes within the wpf form's viewmodel?
EDIT: i saw that you have different viewmodels. now it depends of the use of your usercontrol and the use of mvvm:)
you can use messenger or eventaggregator to comunicate the seleteditems from usercontrolviewmodel to mainviewmodel.
you can also use RelativeSource binding in your usercontrol to bind the selecteditem to your mainviewmodel directly (usercontrol then is just a composition of controls).
you can can rid of the usercontrol viewmodel and put all in the mainviewmodel and take my old example
you can create DependencyProperties for the SelectedItems in your usercontrol!(not usercontrol viewmodel!) and bind these to the properties in your mainviewmodel. i think thats the cleanest way if the usercontrol should be a real usercontrol.
old example:
in your viewmodel: //the real code should of course implement INotifyPropertyChanged and raise it properly
public ObservableCollection<string> MyFirstCollection {get; set;}//init once, add,remove,clear to alter
public string MySelectedCombobox1Value {get;set;}
in your usercontrol:
<ComboBox ItemsSource="{MyFirstCollection }" SelectedItem="{Binding MySelectedCombobox1Value, Mode=TwoWay}" />
thats all relating to your question. be sure that you set the DataContext right. you can check this with tools like snoop. the code i posted expected that the dataconext for the combobox is the viewmodel.
The UserControl should inherit the data context of the form you're adding it to which would be the view model. Any bindings in the UserControl would then be relative to the inherited data context. Have you tried binding to a view model property to ComboBox.Text?
UPDATE
Sorry, misread your question. Didn't see that the user control already has its own view model.
While it seems like there's a better approach, you could expose dependency properties on the user control that exposé the text of each combobox. Just thinking out loud.
The only clean way to do this is with binding, and the only way that would be recommended is if the user control exposes a DependencyProperty for the ViewModel or the individual text properties (as was suggested by sellmeadog) for consumption. Then you can have a property in the parent ViewModel that binds directly to that Dependency Property.

Resources