gpt4 book ai didi

c# - 为什么 VisualTreeHelper.GetChildrenCount() 为 Popup 返回 0?

转载 作者:行者123 更新时间:2023-11-30 13:27:20 25 4
gpt4 key购买 nike

我将焦点移至打开的 Popup:

wcl:FocusHelper.IsFocused="{Binding RelativeSource={RelativeSource Self}, Path=IsOpen}"

FocusHelper类代码:

public static class FocusHelper
{
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused", typeof(bool?), typeof(FocusHelper), new FrameworkPropertyMetadata(IsFocusedChanged));

public static bool? GetIsFocused(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}

return (bool?)element.GetValue(IsFocusedProperty);
}

public static void SetIsFocused(DependencyObject element, bool? value)
{
if (element == null)
throw new ArgumentNullException("element");

element.SetValue(IsFocusedProperty, value);
}

private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)d;

if (e.OldValue == null)
{
fe.GotFocus += ElementGotFocus;
fe.LostFocus += ElementLostFocus;
fe.IsVisibleChanged += ElementIsVisibleChanged;
}

if (e.NewValue == null)
{
fe.GotFocus -= ElementGotFocus;
fe.LostFocus -= ElementLostFocus;
fe.IsVisibleChanged -= ElementIsVisibleChanged;
return;
}

if ((bool)e.NewValue)
{
fe.SetFocusWithin();
}
}

private static void ElementIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var fe = (FrameworkElement)sender;

if (fe.IsVisible
&& (bool)(((FrameworkElement) sender).GetValue(IsFocusedProperty))) // Bring focus to just become visible element.
fe.Focus();
}

private static void ElementGotFocus(object sender, RoutedEventArgs e)
{
((FrameworkElement)sender).SetCurrentValue(IsFocusedProperty, true);
}

private static void ElementLostFocus(object sender, RoutedEventArgs e)
{
((FrameworkElement)sender).SetCurrentValue(IsFocusedProperty, false);
}

/// <summary>
/// Tries to set focus to the element or any other element inside this one.
/// Tab index is respected
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public static bool SetFocusWithin(this DependencyObject element)
{
if (element == null)
throw new ArgumentNullException("element");

var inputElement = element as IInputElement;
if (inputElement == null || !inputElement.Focus())
{
var children = element.GetVisualChildrenSortedByTabIndex().Where(child => !(child is Control) || (bool)child.GetValue(Control.IsTabStopProperty) );
return children.Any(SetFocusWithin);
}

return true;
}
}

ElementTreeHelper 类部分:

public static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject parent)
{
if (parent == null)
throw new ArgumentNullException("parent");

var count = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < count; i++)
yield return VisualTreeHelper.GetChild(parent, i);
}

public static IEnumerable<DependencyObject> GetVisualChildrenSortedByTabIndex(this DependencyObject parent)
{
if (parent == null)
throw new ArgumentNullException("parent");

return parent.GetVisualChildren().OrderBy(KeyboardNavigation.GetTabIndex);
}

问题是var count = VisualTreeHelper.GetChildrenCount(parent) == 0 当parent是Popup时。

更新

答案是here

最佳答案

Popup 本身并不托管 Child。相反,它会创建一个 PopupRoot(内部类),它是一个新的 HWND 的根视觉对象,该 HWND 是为在单独的顶级窗口(或 xbap 中的子窗口)中托管弹出窗口的内容而创建的。 Popup 的子项托管在该 PopupRoot 中的 AdornerDecorator 中。

关于c# - 为什么 VisualTreeHelper.GetChildrenCount() 为 Popup 返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12304904/

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