【问题标题】:How to scroll automatically to a specific item in a wrappanel?如何自动滚动到包装面板中的特定项目?
【发布时间】:2023-03-10 17:30:01
【问题描述】:

目前我在一个大型软件项目中进行协作,但我在尝试实现一个不错的功能时遇到了问题。

问题是,我不知道如何自动滚动到某个用户可以选择的特定项目。包装面板用作项目控件中的项目面板模板。

为了更好的理解,代码如下:

<ItemsControl ItemsSource="{Binding SomeData}">
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <WrapPanel />
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <SomeChart DataContext="{Binding }" Focusable="True" />
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

【问题讨论】:

  • 抱歉我的错误解释我的问题不是我无法设置焦点。我的问题是自动滚动到所选项目以便显示。列表比显示的要长,此时您必须手动滚动。

标签: c# scroll wrappanel


【解决方案1】:

您提到了ItemsControl,大多数从 ItemsControl 派生的控件都有一个 ScrollIntoView 方法来滚动列表框/数据网格到当前选定的项目。

例如:

listBox1.ScrollIntoView(listBox1.SelectedIndex);

dataGrid1.ScrollIntoView(dataGrid1.SelectedIndex);

这是ListView 的另一种解决方案。

更新: 带有 Wrappanel 的列表框

<ListBox x:Name="myList">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel ItemHeight="150" ItemWidth="150"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</Listbox>

【讨论】:

  • 是的,像 scrollintoview 方法正是我需要的,但 wrappanel 没有这样的方法。我可以使用哪些包装板替代品?在项目控件中??
  • 我不确定您要做什么,但为什么不在 ListBox 而不是 ItemsControl 中放置一个包装面板呢?在我的答案中添加了示例
  • 对不起,这是我的错。我不知道列表框是itemscontrol 的替代品。感谢您的帮助,这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
相关资源
最近更新 更多