0

I want to add an item to listview then select and scroll to it. but scroll doesn't move to the item. when I try to add many item, scroll moves to an item but it is not the last added one. It seems listviewItem doesn't generate immediately after adding item using binding.

I tried to add an item then select it (using binding) and scroll to last selected item. But it do not work because count of selected items will be zero.

if (lV.SelectedItems.Count > 0)
{
// do it here (but it will not run because lV.SelectedItems.Count is zero)
}

I also tried to get item from binding but returned value will be null.

ListViewItem item = (ListViewItem)lV.ItemContainerGenerator.ContainerFromItem(lastItem);
//but item will be null

Note that I also used VirtualizingPanel.IsVirtualizing="False" as recommended here.

As select by binding works well I considered binding focus but IsFocused property only lets get not set. So how can I do it?

=========================================================

Edit: I have following codes in xaml:

    <local:PrefectListView x:Name="lV" 
                                          AllowDrop="true"
                                          ScrollViewer.HorizontalScrollBarVisibility="Disabled" 

   VirtualizingPanel.IsVirtualizing="False"
                                          SelectionChanged="lVImage_SelectionChanged"
                                          PreviewKeyDown="lV_PreviewKeyDown"
                                          Drop="lV_Drop"
                                          DragEnter="lV_DragEnter" 
                                          ItemDragsAndDropedToPC="lV_ItemDragsAndDropedToPC" >

                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid  Width="80" Height="120" >
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="80"/>
                                                <RowDefinition Height="40"/>
                                            </Grid.RowDefinitions>
                                            <Image Grid.Row="0" Source="{Binding Thumbnail}" Margin="5"/>
                                            <TextBlock Grid.Row="1" Text="{Binding Name}" Margin="5,0,5,5" HorizontalAlignment="Center" VerticalAlignment="Top"
                                                       TextWrapping="Wrap"  TextTrimming="CharacterEllipsis"/>
                                        </Grid>
                                    </DataTemplate>
                                </ListView.ItemTemplate>

                                    <ListView.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <WrapPanel Orientation="Horizontal"/>
                                        </ItemsPanelTemplate>
                                    </ListView.ItemsPanel>

                                <ListView.ItemContainerStyle>
                                    <Style TargetType="ListViewItem">
                                        <EventSetter Event="MouseDoubleClick" Handler="lVItem_MouseDoubleClick"/>
                                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                                        <Setter Property="Margin" Value="5,5,2,2"/>
                                    </Style>
                                </ListView.ItemContainerStyle>

                            </local:PrefectListView>

In cs, after creating item I add it to _navigationSources and then use following code to select and scroll to it:

private void SelectItems(bool clearPreviousSelecteds, bool focus, NavigationPanItemRow[] rows)
        {
            //force to run it on UI thread
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                //check arguments
                if (rows == null) { throw new ArgumentNullException("rows"); }
                //
                for (int i = 0; i < rows.Length; i++)
                {
                    if (rows[i] == null)
                    { throw new ArgumentException("Atleast one of members is null", "rows"); }
                }

                //variables
                NavigationPanItemRow lastItem = null;

                //clear all selected
                if (clearPreviousSelecteds)
                {
                    _navigationSources.UnSelectAll();
                }

                //select
                for (int i = 0; i < rows.Length; i++)
                {
                    rows[i].IsSelected = true;
                    lastItem = rows[i];
                }

                //focus (I use it ) 
                if (focus && lastItem != null)
                {
                    lV.Focus();
                    ListViewItem it = (ListViewItem)lV.ItemContainerGenerator.ContainerFromItem(lastItem);
                    lV.ScrollIntoView(it);
                }

                //focus (also it)
                if (focus && lV.SelectedItems.Count > 0)
                {
                    lV.Focus();
                    ((ListViewItem)lV.ItemContainerGenerator.ContainerFromItem(lV.SelectedItems[lV.SelectedItems.Count - 1])).Focus();
                }
            }));
        }

Note: if I add a messagebox before focus part, As it will provide some time, code works well.

NewComer
  • 11
  • 4
  • Hi NewComer, welcome to Stackoverflow! It is not clear how you hooked up the event handlers in your code. In order for us to help you, please provide all the relevant c# and xaml code in your question. – Tobias Hoefer Mar 10 '19 at 11:00
  • @Tobias Hoefer, thanks for your comment. I provided some codes. – NewComer Mar 10 '19 at 15:11
  • At last I used a timer and selected items in tick event. it is not prefect but it works. – NewComer Apr 11 '19 at 06:36

0 Answers0