简体   繁体   English

自动向下滚动以查看列表中的选定项目

[英]Automatic scroll down to view the selected item in the list

I am using MVVM.. I am having a List of xml Nodes in property InputTemplates. 我正在使用MVVM。我在属性InputTemplates中有一个XML节点列表。 I want the list to be scrolled down automatically, when i select any item of the list from my view model. 当我从视图模型中选择列表的任何项目时,我希望列表自动向下滚动。 I know that i have to use a property "SelectedItem" in my view Model that will help. 我知道我必须在视图模型中使用属性“ SelectedItem”,这将有所帮助。

<Border BorderBrush="{StaticResource BorderBrush}" 
IsEnabled="{Binding Path=InputTemplateBorderEnabled}" 
BorderThickness="2" CornerRadius="5" Canvas.Left="1" Canvas.Top="84" Height="240" Name="border7" Width="432" >
      <HeaderedContentControl    
                Content="{Binding Path=InputTemplates,Mode=OneTime}"
                Header="{Binding Path=INTemplateLabel}"
                ContentTemplate="{StaticResource FileTabTemplate}"
                Style="{StaticResource MainHCCStyle}" Width="420" Height="237" />
</Border>
 <DataTemplate x:Key="FileTabTemplate">
        <st:ScrollableTabControl Background="#FFF0F9F8"  
          IsSynchronizedWithCurrentItem="True" 
          ItemsSource="{Binding}" 
          ItemTemplate="{StaticResource FileTabItemTemplate}"                 
          Margin="1">
        </st:ScrollableTabControl>
</DataTemplate>
<DataTemplate x:Key="FileTabItemTemplate" >       
        <DockPanel>
            <TextBlock Name="textBlock" Text="{Binding Path=Keyword}" ToolTip="{Binding Path=FileName}"  FontFamily="Microsoft Sans Serif" FontSize="10"  TextWrapping="NoWrap"/>
        </DockPanel>

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                <Setter TargetName="textBlock" Property="Foreground" Value="Indigo"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
ObservableCollection<CommonResource.ViewModel.FileTemplateViewModel> inputTemplates;

foreach (XMLTemplateViewModel xmlvm in inputTemplates)
{
   list = xmlvm.XMlRootNodes[iSearchRootNode];
   list.SelectedItem = MyList[iSelectionIndex];// MyList is a list of few items TreeViewWithIcons
}

On setting list.SelectedItem, the selected item should be visible in the screen after automatic scrolling down. 在设置list.SelectedItem上,自动向下滚动后,所选项目应在屏幕上可见。 Do i have to use any event for this? 我需要为此使用任何事件吗? Please also provide the code for property "SelectedItem". 还请提供属性“ SelectedItem”的代码。

When you are using a ListBox or ListView you could try this: 当您使用ListBox或ListView时,可以尝试以下操作:

public class ScrollIntoViewBehavior:Behavior<ListBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
    }

    void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            (sender as ListBox).ScrollIntoView(e.AddedItems[0]);
        }
    }
}

Change the logic to whatever you want :) 将逻辑更改为您想要的任何内容:)

May be this link can help... 可能此链接可以帮助...

http://kiwigis.blogspot.in/2010/12/how-to-add-scrollintoview-to.html http://kiwigis.blogspot.in/2010/12/how-to-add-scrollintoview-to.html

Bascially you would ned to find out the item container of your ItemsControl such as ListBoxItem , TreeViewItem , ComboBoxItem , ListViewItem , DataGridRow etc. and call its BringIntoView() function. 通常,您需要查找ItemsControl的项目容器,例如ListBoxItemTreeViewItemComboBoxItemListViewItemDataGridRow等,并调用其BringIntoView()函数。

In MVVM, this can be via attached behaviors. 在MVVM中,这可以通过附加行为来实现。

This is the format of data in the XML item view. 这是XML项目视图中的数据格式。 I want to know that how to add a trigger such that when HeaderTextBlock is selected, the scroll bar moves down to make selected item visible. 我想知道如何添加触发器,以便在选择HeaderTextBlock时,滚动条向下移动以使所选项目可见。

                <HierarchicalDataTemplate  DataType="{x:Type cr:TreeViewWithIcons}" ItemsSource="{Binding Path=ChildNodes,Mode=OneTime}">                       
                        <StackPanel Orientation="Horizontal" Name="stackpanel" IsEnabled="False" >
                        <Image Source="{Binding Path=Icon}"/>
                        <TextBlock Name="HeaderTextBlock" Text="{Binding Path=HeaderText}" Background="{Binding Path=BackgroundColor,Mode=TwoWay,NotifyOnSourceUpdated=True}" Foreground="{Binding Path=HeaderColor,Mode=TwoWay,NotifyOnSourceUpdated=True}" IsEnabled="False">
                        </TextBlock>
                    </StackPanel>
                    <HierarchicalDataTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType= {x:Type TreeViewItem}},Path=IsSelected}" Value="True">
                            <Setter TargetName="HeaderTextBlock" Property="Foreground" Value="Red"/>
                            <Setter TargetName="stackpanel" Property="Background" Value="LightGray"/>
                            <Setter TargetName="HeaderTextBlock" Property="Background" Value="LightGray"/>
                            <Setter Property="BitmapEffect">
                                <Setter.Value>
                                    <OuterGlowBitmapEffect GlowColor="Black" />
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                            <Setter TargetName="HeaderTextBlock" Property="Foreground" Value="Purple"/>
                            <Setter TargetName="HeaderTextBlock" Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </HierarchicalDataTemplate.Triggers>
                </HierarchicalDataTemplate>
            </ResourceDictionary>
        </cr:ExtendedTreeView.Resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM