Scrolling according to the selected item in the stack panel

I have a Model that communicates with ItemsControl. Inside the ItemsControl element, I have a stack panel. Now I want the selected item to be changed using the arrow keys. Like in the attached picture, I have the 1st item selected, and when I click on the 4th item, it should be selected. The problem is that the elements in the line depend on the screen resolution, so on some screen there are 4 elements per line, and some have three. Secondly, when I go to the point where the page ends, the scroll should go down. How can i achieve this?

Here is my xaml:

<ScrollViewer  HorizontalAlignment="Center" VerticalScrollBarVisibility="Auto" Width="Auto" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="2"  VerticalAlignment="Top" Margin="0,10,10,0">
            <ItemsControl  Name="productVariants">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"  />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <DockPanel MouseLeftButtonDown="ProductVariantClicked" Tag="{Binding VariantCBX}" Margin="8" MaxHeight="160" MaxWidth="200" MinWidth="200" MinHeight="160">
                            <Border Name="ItemBorder" CornerRadius="6" BorderBrush="Gray" Background="White" BorderThickness="2" DockPanel.Dock="Top">
                                <StackPanel Margin="0">

                                    <TextBlock Name="ProductVariantOption" Text="{Binding VariantOption}" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20"/>
                                    <Image Source="{Binding ProductVariantLogoPath}" Height="80" Width="180" />
                                    <TextBlock Text="{Binding VendorName}" HorizontalAlignment="Center" FontSize="15" FontWeight="Bold" />
                                    <TextBlock Text="{Binding SellingPrice}" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold" Foreground="Red" />

                                </StackPanel>
                            </Border>
                        </DockPanel>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsSelected}" Value="True" >
                                <Setter TargetName="ItemBorder" Property="BorderBrush" Value="Yellow"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>

            </ItemsControl>

        </ScrollViewer>

Stack panel with scroll

+4
source share
1

, , . ActualWidth (, "ItemBorder" Border). ActualWidth ( ) ActualWidth ItemsControl, , . - , ( ), , .

ActualWidths, . - , , . - "" , , .

+1

Source: https://habr.com/ru/post/1655739/


All Articles