gpt4 book ai didi

c# - WPF TreeView : Why does virtualizing break BringIntoView()?

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:02 24 4
gpt4 key购买 nike

当您在 WPF 中的 TreeView 上设置 VirtualizingStackPanel.IsVirtualizing="True" 时:

<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView Name="MainTree" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"/>
</Grid>
</Window>

调用 TreeViewItem.BringIntoView() 的以下代码隐藏不起作用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TreeViewItem lastTvi2 = null;
for (int i = 0; i < 100; i++)
{
TreeViewItem tvi = new TreeViewItem();
tvi.Header = "Hello";
MainTree.Items.Add(tvi);
for (int j = 0; j < 100; j++)
{
TreeViewItem tvi2 = new TreeViewItem();
tvi2.Header = "World";
tvi.Items.Add(tvi2);
lastTvi2 = tvi2;
}
}
lastTvi2.BringIntoView();
}
}
}

然而,当您删除 VirtualizingStackPanel.IsVirtualizing="True" 行时,它会将 lastTvi2 对象(树中的最后一项)带入 View 。为什么这会破坏 TreeViewItem.BringIntoView() 方法?这是正常行为,还是 .NET 错误?

最佳答案

我曾多次偶然发现这个问题,并提出了一个使用标准 treeview 和标准 treeviewitem 对我有用的解决方案

<UserControl
...
<TreeView
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling"/>
...
</UserControl>

public static class TreeViewHelper
{
private static T FindVisualChild<T>(System.Windows.Media.Visual visual) where T : System.Windows.Media.Visual
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
{
System.Windows.Media.Visual child = (System.Windows.Media.Visual)VisualTreeHelper.GetChild(visual, i);
if (child != null)
{
T correctlyTyped = child as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}

T descendent = FindVisualChild<T>(child);
if (descendent != null)
{
return descendent;
}
}
}

return null;
}

public static void BringIntoView(TreeViewItem item)
{
ItemsControl parent = item.Parent as ItemsControl;

if (parent != null)
{
System.Windows.Controls.VirtualizingStackPanel itemHost = FindVisualChild<System.Windows.Controls.VirtualizingStackPanel>(parent);

if (itemHost != null)
{
itemHost.BringIndexIntoViewPublic(parent.Items.IndexOf(item));
item.Focus();
}
}
}
}

关于c# - WPF TreeView : Why does virtualizing break BringIntoView()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29837763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com