2 Replies - 217 Views - Last Post: 24 June 2020 - 04:33 AM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 333
  • Joined: 13-March 12

Programmatically select an item in a ListView

Posted 23 June 2020 - 10:42 AM

WPF MVVM C#


I want to synchronize two ListViews. When the user selects an item in the "Little" listview, I want the same object shown in the "Big" list view. I can select the item but I don't know how to make it the visible item on the screen. In the "Big" listview only one item is visible at a time. In the little list view several are visible, but they are small, with limited information. Kind of like an index.

Here is the XAML.
            <ListView ItemsSource="{Binding Tickets}"
                          IsSynchronizedWithCurrentItem="True"
                          SelectedItem="{Binding TicketSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          VerticalAlignment="Stretch"
                          HorizontalAlignment="Stretch"
                          HorizontalContentAlignment="Center"
                          Background="Transparent"
                          ScrollViewer.VerticalScrollBarVisibility="Auto">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <Border x:Name="ItemBorder">
                                        <ContentPresenter />
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="ItemBorder" Property="BorderBrush" Value="Red" />
                                            <Setter TargetName="ItemBorder" Property="BorderThickness" Value="1" />
                                        </Trigger>
                                        <Trigger  Property="IsSelected" Value="True">
                                            <Setter TargetName="ItemBorder" Property="Background" Value="Blue" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>



Then, in the ViewModel I have
        //This event is fired in the "Little" list ViewModel. Steppping through shows everything here works as designed.
        private void OnFindTicketCommand(object sender, TicketEventArgs e)
        {
            string firstName= e.IDString.Split('-').First().Trim();
            string lastName= e.IDString.Split('-').Last().Trim();
            TicketSelected=Tickets.First(x => x.FirstName==firstName && x.LastName==lastName);
            SetActiveTicket();
        }

        public TicketViewModel TicketSelected
        {
            get { return _selectedTicket; }
            set
            {
                if (value != null)
                {
                    _selectedTicket = value;
                    _selectedTicket.IsSelected = true;
                    RaisePropertyChanged();
                }
            }
        }

        public void SetActiveTicket()
        {
            //I can step through here and every line gets executed. But the "MoveToCurrent" doesn't move to current.
            ICollectionView colView = CollectionViewSource.GetDefaultView(this.Tickets);
            if (colView != null)
                colView.MoveCurrentTo(TicketSelected);
        }




I am sure there is something wrong with the CollectionView, or rather, the way I am trying to use it. Or maybe there is a completely different and easy way to do this.

Thank you for your help.

Edit: to clarify. The click event gets fired in the little listview but the event handler is in the MainWindowViewModel where the two listviews live.

This post has been edited by ScottinTexas: 23 June 2020 - 10:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Programmatically select an item in a ListView

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7447
  • View blog
  • Posts: 25,085
  • Joined: 05-May 12

Re: Programmatically select an item in a ListView

Posted 23 June 2020 - 01:46 PM

Sounds like all you need to do with the "Big" listview is to scroll the now current item into view. Unfortunately, it looks like one does not just simply scroll into view if you follow MVVM strictly. You would need to create a behavior if you want to keep playing within the MVVM rules, or break down and do some code behind.

See SO.

I'm curious though: If the "Big" listview can ever only display only a single at a time, why even make it a list view? Why not just have a user control that displays a single item's details?
Was This Post Helpful? 1
  • +
  • -

#3 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 333
  • Joined: 13-March 12

Re: Programmatically select an item in a ListView

Posted 24 June 2020 - 04:33 AM

View PostSkydiver, on 23 June 2020 - 03:46 PM, said:

I'm curious though: If the "Big" listview can ever only display only a single at a time, why even make it a list view? Why not just have a user control that displays a single item's details?


Thanks Skydiver.

Such as? Big list is "all work" little list is work in progress. Several (maybe 4) users can select from Big list and the little list reflects what the users have selected. Being able to click on the individual user's item to bring up the big item in the long list seemed like the right thing to do. The problem is, the workflow hasn't been tested yet. After working on this, I think little list should be used to 'Find' the work and big list should be a panel with up to 4 'in progress' jobs. In which case big list would be a different control.

I used a list view because it is so easy to just add the ViewModel to the collection and it appears as designed. I am not sure what controls will allow me to do the same thing. That is, using DataType to make the control appear whenever the viewmodel is added to the containing control. So what control can I add a viewmodel to? Maybe a panel, then figure out the code to have a grid of 2 X 2 to display the views. I will have to do some reading.

Fortunately I am using the MVVM pattern and it makes making changes like that easy. Mostly in the XAML.

This post has been edited by ScottinTexas: 24 June 2020 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1