I'm don't know how to get focused item automatically change within a ListView.
I would like the focused item in the view to automatically change when I change the "IsSelected" property to an other element in the databinded list:
When an item is modified by PC/SC card reader (see this as input), the next element should be focused like this:
I would like to stay in MVVM and therefor not having View referenced in the ViewModel. Below is my current code.
Model : The main purpose is to extend a DTO with an IsSelected property and implementing INotifyPropertyChanged
public class SmartDeviceModel : INotifyPropertyChanged { public bool IsSelected; private DtoReader _dtoReader; public SmartDeviceModel(DtoReader _reader) { _dtoReader = _reader; } public string DisplayName => _dtoReader.DisplayName; public string Uid { get { return _dtoReader.Uid; } set { _dtoReader.Uid = value; OnPropertyChanged("Uid"); } } public long RadioId { get { return _dtoReader.RadioId : _dtoMarker.RadioId; } set { _dtoReader.RadioId = value; OnPropertyChanged("RadioId"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } ViewModel received events of a PC/SC card reader to pair data from RFID chip with current selected item. When RFID chip is removed from PC/SC Reader, the next element is well selected but got not focused.
public class ScanDeviceViewModel : BaseViewModel { public BindingList<SmartDeviceModel> ReaderList { get; } public int SelectedReaderIndex; private ITagReaderInput _rfidReader; public ScanDeviceViewModel() { //Get Data listener for RFID Tag _rfidReader = new IdentivTagReader.IdentivTagReader(); // Data Source of DTO SiteInteractor siteInterractor = new SiteInteractor(); // List used for DataBinding ReaderList = new BindingList<SmartDeviceModel>(); foreach (DtoReader m in SiteInteractor.GetReaders().OrderBy(x => x.DisplayName)) { ReaderList.Add(new SmartDeviceModel(m)); } if (ReaderList.Count() > 0) { for (var i = 0; i < ReaderList.Count(); i++) { if (String.IsNullOrEmpty(ReaderList[i].Uid)) { SelectedReaderIndex = i; ReaderList[i].IsSelected = true; break; } } } _rfidReader.LabelDetected += RfidTagDetected; _rfidReader.LabelRemoved += RfidRemoved; } private void RfidTagDetected(ITagLabel tag) { if (ReaderList[SelectedReaderIndex] != null && string.IsNullOrEmpty(ReaderList[SelectedReaderIndex].Uid)) { ReaderList[SelectedReaderIndex].IsSelected = true; ReaderList[SelectedReaderIndex].Uid = tag.Uid; ReaderList[SelectedReaderIndex].RadioId = tag.RadioId; } } private void RfidRemoved(ITagLabel tag) { if (ReaderList[SelectedReaderIndex].Uid == tag.Uid) { ReaderList[SelectedReaderIndex].IsSelected = false; while (ReaderList.Count >= SelectedReaderIndex + 1) { SelectedReaderIndex++; if (String.IsNullOrEmpty(ReaderList[SelectedReaderIndex].Uid)){ ReaderList[SelectedReaderIndex].IsSelected = true; break; } } } } } View I'm using a "Setter" using databinding to my model property "IsSelected" as suggested here but I most missed something else I don't understand yet.
<ListView ItemsSource="{Binding ReaderList}" Margin="5" x:Name="listViewReader" SelectionMode="Single" <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="BorderBrush" Value="LightGray" /> <Setter Property="BorderThickness" Value="0,0,0,1" /> <Setter Property="IsSelected" Value="{Binding IsSelected}" /> </Style> </ListView.ItemContainerStyle> <ListView.ItemTemplate> <DataTemplate> <Viewbox Grid.Row ="0" Stretch="Uniform" HorizontalAlignment="Left" VerticalAlignment="Bottom" MaxHeight="90"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="2*" /> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Label Content="{Binding DisplayName}" /> <DockPanel Grid.Row="1"> <Label Content="UID"/> <Label Content="{Binding Uid}"/> </DockPanel> <DockPanel Grid.Row="1" Grid.Column="1"> <Label Content="RadioID" /> <Label Content="{Binding RadioId}"/> </DockPanel> </Grid> </Viewbox> </DataTemplate> </ListView.ItemTemplate> </ListView> I tried several approach like this answer, although item is well selected, it is not focused.

