Set focus to List item on Listview - wpf

I have a Listview her ItemsSource property is binded to ViewModel, when i add a new item to List (ObservableCollection) i need set focus to the new item.

Bind the SelectedItem property of the ListView to a property in your ViewModel and when you add a new item to the collection set the property in your ViewModel to reference that new item and the binding will take care of the rest.

Related

Cannot clear selection of ListView

In my listview, when I select on of the item I will show a new view and set the listview to not select any item by set SelectedItem to be null. But the listview still select the old item that I selected. According to this link, I have set
IsSynchronizedWithCurrentItem="True"
But it still same. My item list is compose from the ItemViewModel that inherited from MvxViewModel
Are you using ListView(ListBox) or any custom control?
A class inherited from MS ListBox (such as ListView) has a static method to unselect all selected items:
ListBox.UnselectAll()
To unselect only one item you can cast selected item to ListBoxItem object and call:
ListBoxItem item = (ListBoxItem)obj;
item.IsSelected = false;
Have you tried it in your code behind? Or you want to achieve this declaratively by XAML markup?

Can I add a new item placeholder to ListBox in XAML?

If I have a WPF ListBox bound to a collection, how can I add a new item placeholder to the end?
I understand that IEditableCollectionView.NewItemPlaceholderPosition might provide this, but I am unclear on how to configure it through XAML.
If I have a WPF ListBox bound to a collection, how can I add a new item placeholder to the end?
If you have a collection data bound to your ListBox.ItemsSource property, then to add a new item to the end, you simply add a new, empty object to that collection:
YourCollectionProperty.Add(new YourDataType());
If you have a DataTemplate to define what your items look like and have a property on your data type class that is displayed, you could fill that property with whatever new item text that you wanted to:
YourCollectionProperty.Add(new YourDataType() { YourDisplayedProperty = "New Item" } );

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

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 FrameworkElementFactory to data object property

I have a ListBox whose DataTemplate is created in code using 3 FrameworkElementFactory objects(A StackPanel with 2 appended children(CheckBox and TextBox)). The item object in the collection that is bound to the ItemsSource of the ListBox is basically the same type of Item object that you would typically see with any type of ListControl. What I'm trying to do is bind the CheckBox's IsChecked property in the DataTemplate to a boolean property on the Item object. The ListBox supports 3 modes, single select, multiselect, and multicheck. The mode I'm trying to implement is multicheck so that the IsChecked property of the CheckBox is bound to the Selected property of the item object. This creates a behavior where the item is only considered selected when the CheckBox's IsChecked property on the ListBoxItem is true, not when the WPF ListBoxItem's IsSelected property is true. What should happen is that the boolean property on the data object should be bound to the IsChecked property, and when the IsChecked property is changed the Selected property on the item object will update, and will thus update a SelectedItems collection behind the scenes.
Here is some simplified code that I have just described.
ListBox innerListBox = new ListBox();
//The ItemsSource of the ListBox being set to the collection of items
this.innerListBox.ItemsSource = this.Manager.ItemManagers;
this.innerListBox.ItemTemplate = this.GetMultipleCheckTemplate();
public System.Windows.DataTemplate GetMultipleCheckTemplate()
{
DataTemplate dt = new DataTemplate;
FrameworkElementFactory factorySP = new FrameworkElementFactory(typeof(StackPanel));
FrameworkElementFactory factoryCB = new FrameworkElementFactory(typeof(CheckBox));
factoryCB.SetBinding(CheckBox.IsCheckedProperty, new Binding("Selected");
RoutedEventHandler clickHandler = new RoutedEventHandler(ItemCheckBox_Click);
factoryCheckBox.AddHandler(CheckBox.ClickEvent, clickHandler, true);
factorySP.AppendChild(factoryCB);
FrameworkElementFactory factoryTB = new FrameworkElementFactory(typeof(TextBlock));
factoryTB .SetBinding(TextBlock.TextProperty, new Binding("Description");
factorySP.AppendChild(factoryTB);
template.VisualTree = factorySP;
return template;
}
There is some code that I'm not including that is the event handler on the CheckBox. If there is a multiple selection on the Wpf ListBox, then all of the CheckBoxes in the range would be toggled to the value of the CheckBox that was clicked. I can manually set the Selected property on the Item to the IsChecked property of the sender and everything works fine, I would however think that databinding should just work and I wouldn't have to do this manually. Would the databinding in this case be asynchronous or do I need to do something explicitly?

Resources