Double databinding - wpf

I am just learning WPF (and mvvm), and I have encountered a problem which I can't google through.
I have 2 ObservableCollections - exercises and charts (the project is about trackig progress in a gym):
Exercise (Id, Name)
Chart (ExerciseId, ExerciseName, Id, ...)
Now in a window where I want to fill the charts, I have a listbox with some labels and a _grid_ and a combobox in ItemTemplate.
Listbox is binded to Chart collection.
Combobox shows a list of exercises, so I am binding it to Exercise collection.
Questions:
Can I specify in xaml that combobox current value should be same as Chart.Exercise?
How can I specify a binding in XAML so Exercise collection element from a combobox would be assigned to Chart.Exercise?

You could possibly achieve this with Element Binding and a Converter... However, it would be simpler to achieve this in the ViewModel.
You would have four Properties in your ViewModel.
Property 1: Exercise Observable Collection - Bound to your ComboBox ItemSource (ExerciseItems)
Property 2: Exercise Selected Item - Bound to your Combobox SelectedItem (ExerciseSelectedItem)
Property 3: Chart Observable Collection - Bound to your ListBox (ChartItems)
Property 4: Chart Selected Item - Bound to your ListBox SelectedItem (ChartSelectedItem)
You would Set your Combo Box Selected Item, using Linq perhaps, to be equal to the Item with ListBox Selected Item ExerciseID, in the Setter of the ListBox Selected Item Property;
Public Property ChartSelectedItem As ChartItem
Get
Return _ChartSelectedItem
End Get
Set(value As ChartItem)
If value <> _ChartSelectedItem Then
_ChartSelectedItem = value
ExcersiseSelectedItem = (From ExcersiseItemsList in ExcersiseItems Where ExcersiseItemsList.ID = value.ExcersiseID).FirstOrDefault
OnPropertyChanged("ChartSelectedItem")
End if
End Set
End Property
Hope that helps...

Related

Selecteditem event in MVVM silverlight

i have a datagrid bound to a property. In this grid i have columns which consists of cells which are like hyperlink i mean when user clicks on the cell value based on these values another gird will get populated. i want to know how to get the cell value and pass it to some method so that other grid will get populated.
The best way to do this is in your viewmodel.
You should bind the SelectedItem of your datagrid to a new property in your ViewModel. In the set method of this new Property, call a new method to populate a new ObservableCollection/List/whatever...
Finally, bind your "other grid" ItemsSource to this new observable collection from your ViewModel.
Edit:
If you need to load one thing or another depending on the column you are going to use the code behind, take a look at this:
Silverlight DataGrid how to get cell value from a selected item?

How Do I Change WPF Listview SelectedItem Font Color With ItemSource Bound?

I have WPF window containing a listview that has it's itemsource set to a collection of objects. When I access SelectedItem or SelectedItems[] or Items[], I get the my object back that's bound to that item, not the ListViewItem item itself. I have no idea how to select a row and change it's color since I can't access the item itself, like a winform listviewitem.
ListView derives from ItemsControl which exposes the ItemContainerGenerator property. This object allows you to map a bound entity to its ItemContainer (the item your are looking for) and back.

Binding to an ObservableCollection of UserControls

Simple Silverlight question: I have an ObservableCollection<MyObject> in my viewmodel. Every MyObject has a Label property. If I bind a ListBox to the collection and set DisplayMemberPath to Label, or set the ItemTemplate to a TextBlock that binds the Text property to Label, all works as expected.
If I change MyObject so it derives from a UserControl, the Label text no longer shows up in the ListBox; each item just shows up as a blank strip a few pixels tall. Why is this? There's obviously something I'm missing here about how different things get rendered.
The ListBox determines that the set of items in its ItemsSource are already UIElement instances and therefore decides to use those elements directly as the content of the ListBoxItem elements it creates.

Data Binding with WPF and MVVM/Model-View-ViewModel

I'm having difficulty correctly Data Binding my models in the view. I have a MainWindowViewModel which contains a list of AlbumViewModels. Each AlbumViewModel holds an AlbumModel. So I have multiple albums and I need to display information in an ObservableCollection in the AlbumModel. I have two ListBoxes. ListBox1 holds the list of AlbumViewModels that are in my MainWindowViewModel. My second ListBox I want to display the ObservableCollection from the current selected item from the AlbumViewModel.AlbumModel. How can I do this? I've tried binding the DataContext of ListBox2 to the ListBox1 element, along with SelectedItem as the path but that returns 'AlbumViewModel'. Is there anyway to bind ItemsSource of a ListBox to the binding of the DataContext, but in this case binding it to [DataContext].AlbumModel.ObservableCollection or something?
I apologise if it sounds rather complicated!
You can use the fact, that when you bind to a collection, WPF wraps collection to CollectionView. And this guy has CurrentItem.. Bea had good article: How can I sync selection of two data bound ListBoxes? and Dr.WPF is amazing (as usual): ItemsControl: 'C' is for Collection.

How do I loop through a set of items in a databound Silverlight control such as the ListBox?

I have a silverlight ListBox that has it's ItemsSource set. From the C# code behind I want to loop through each ListBox item and change the value of the text and access controls that are in the ListBox ItemTemplate. How would I do that?
ListBox controls in silverlight are bound to an ienumerable type, so that if any value in the ListBox changes, the underlying data is changed and vice versa depending on what type of binding you require. To effectively iterate the items you'll want to iterate through the enumerable object you've bound to.
You can get to the collection by accessing the ListBox.ItemsSource property and change the text of appropriate items, perform LINQqueries etc. If you have bound the controls correctly, saving the collection should update the list.
Hope this helps!

Resources