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

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.

Related

Double databinding

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...

CurrentCell is changed to a row that isn't SelectedItem in Datagrid

I have a DataGrid that is part of a DataTemplate that is assigned to the ContentTemplate of a TabControl. The TabControl's ItemsSource is bound to a collection and, as such, the DataContext for the DataGrid changes to a new collection once each Tab is selected. Currently, there are bindings for ItemsSource and SelectedItem on the DataGrid.
When I move through the tabs the DataGrid is able to keep the selected row synchronized properly but the problem I'm having is that CurrentCell is always set to the first column and first row regardless of what SelectedItem equals.
I've tried setting the CurrentCell property when the DataContext changes for the DataGrid but the DataGrid always resets it back to the first row and first column. Does anyone know how I can accomplish keeping the CurrentCell on the same Row as SelectedItem when DataContext changes?
This worked for me (where dg is the DataGrid):
DataGridCellInfo cellInfo = new DataGridCellInfo(dg.SelectedItem, dg.Columns[0]);
dg.CurrentCell = cellInfo;

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.

How to assign value of the dataContext to ListBox control in silverlight?

Hi I have contentControl in my user control. I am applying style to this contentControl which consist of TextBlock and ListBox. I am binding text of the textBlock to CategoryName(from Tag of the control). I want to bind category's child items to listBox. I have set ContentControl's dataContext property to child items[]. Now how to bind these child items to listbox which is in resource.
In loaded event of the user control
Panel pnl = sender as Panel;
Category category = panel.Tag as Category;
Items[] items = GetChildItemsByCategoryId(category.CategoryID);
mainContent.DataContext = items;
If the control is in the visual tree of mainContent, simply set in code or XAML
ItemsSource={Binding}

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