gpt4 book ai didi

WPF 无法使 ItemContainerGenerator.ContainerFromItem 工作

转载 作者:行者123 更新时间:2023-12-02 17:40:51 25 4
gpt4 key购买 nike

我看过herehere和许多其他地方,但我似乎无法让 ItemContainerGenerator.ContainerFromItem 方法在 WPF TreeView 上工作!我试图传递我想看到的实际项目,但没有得到任何结果,我只是试图在我的 TreeView 中获取第一个项目。这是我的示例代码:

private static bool ExpandAndSelectItem(ItemsControl parentContainer, object itemToSelect)
{
// This doesn't work.
parentContainer.BringIntoView();
// May be virtualized, bring into view and try again.
parentContainer.UpdateLayout();
parentContainer.ApplyTemplate();

TreeViewItem topItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(parentContainer.Items[0]);

// Can't find child container unless the parent node is Expanded once
if ((topItem != null) && !topItem.IsExpanded)
{
topItem.IsExpanded = true;
parentContainer.UpdateLayout();
}
}

如您所见,我已尝试调用许多“更新”方法来尝试使 TreeView 变得“可见”和“可访问”。第 22 条军规似乎是你不能使用 ContainerFromItem() 除非第一个 TreeViewItem 被展开,但我不能捕获 TreeViewItem 展开它直到 ContainerFromItem() 有效!

发生的另一件有趣的事情是:当我打开这个窗口(它是一个 UserControl)时,ContainerFromItem() 返回空值,但是如果我关闭窗口并重新打开它,ContainerFromItem() 开始返回非空值。有没有我应该寻找或强制触发的事件?

最佳答案

原来我正在寻找的事件是“已加载”。我只是将一个事件处理程序附加到 XAML 中的 TreeView ,并在该事件处理程序中调用我的逻辑。

<TreeView x:Name="MyTreeView"
Margin="0,5,0,5"
HorizontalAlignment="Left"
BorderThickness="0"
FontSize="18"
FontFamily="Segoe WP"
MaxWidth="900"
Focusable="True"
Loaded="MyTreeView_Load">
...
</TreeView>

事件处理程序:

private void MyTreeView_Load(object sender, RoutedEventArgs e)
{
ShowSelectedThing(MyTreeView, ThingToFind);
}
// Gotta call the TreeView an ItemsControl to cast it between TreeView and TreeViewItem
// as you recurse
private static bool ShowSelectedThing(ItemsControl parentContainer, object ThingToFind)
{
// check current level of tree
foreach (object item in parentContainer.Items)
{
TreeViewItem currentContainer = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
if ((currentContainer != null) && (item == ThingToFind)
{
currentContainer.IsSelected = true;
currentContainer.BringIntoView();
return true;
}
}
// item is not found at current level, check the kids
foreach (object item in parentContainer.Items)
{
TreeViewItem currentContainer = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
if ((currentContainer != null) && (currentContainer.Items.Count > 0))
{
// Have to expand the currentContainer or you can't look at the children
currentContainer.IsExpanded = true;
currentContainer.UpdateLayout();
if (!ShowSelectedThing(currentContainer, ThingToFind))
{
// Haven't found the thing, so collapse it back
currentContainer.IsExpanded = false;
}
else
{
// We found the thing
return true;
}
}
}
// default
return false;
}

希望这对某人有帮助。有时在现实世界中,面对要求苛刻的客户、怪异的要求和紧迫的期限,你得动手了!

关于WPF 无法使 ItemContainerGenerator.ContainerFromItem 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863666/

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