gpt4 book ai didi

wpf - 拦截 RelativeSource FindAncestor

转载 作者:行者123 更新时间:2023-12-04 14:34:05 32 4
gpt4 key购买 nike

我有一个作为 excel 插件运行的 WPF 应用程序,它有这样的可视化树

  • 精益求精
    • 元素主机
      • WPF 用户控件
        • WPF 色带条控件

现在,在 excel 中加载插件时,不会启用位于 WPF 功能区栏控件上的任何控件。请参阅下面的错误

System.Windows.Data Error: 4 : Cannot find source for binding with 
reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=IsActive; DataItem=null; target element
is 'Ribbon' (Name=''); target property is 'NoTarget' (type 'Object')

如果我将功能区栏控件嵌套在独立窗口(在 excel 之外)中,它可以正常工作。

有没有办法拦截 FindAncestor 对窗口的调用并将其连接到其他东西。?请注意,我无法更改上述绑定(bind),因为它不是我的控制权。

最佳答案

最直接的回答

FindAncestor 由 WPF 内部处理,并会在转到其他任何地方之前尽可能地搜索可视化树。只有当它到达一个没有视觉父项的 Visual 时,它才会在别处搜索,这取决于它到达的是什么。例如,如果它命中 FrameworkContentElement,它可以转到文档的容器。不幸的是,如果可视化树的顶部是 ElementHost,它将停止,因此无法重新路由调用。

这意味着您最简单的选择是替换绑定(bind)。幸运的是这不是很困难。

如何自动替换绑定(bind)

这是我不久前写的一个简单方法,它搜索可视化树并按照 updateFunction 的指示替换绑定(bind)。如果 updateFunction 返回的绑定(bind)与传递的绑定(bind)不同,则绑定(bind)会更新。

static void UpdateBindings(Visual visual, Func<Binding, Binding> updateFunction)
{
if(visual==null) return;
for(int i=0; i<VisualTreeHelper.GetChildrenCount(visual); i++)
UpdateBindings(VisualTreeHelper.GetChild(visual, i) as Visual, updateFunction);
for(var enumerator = visual.GetLocalValueEnumerator(); enumerator.MoveNext(); )
{
var property = enumerator.Current.Property;
var binding = BindingOperations.GetBinding(visual, property);
if(binding==null) continue;
var newBinding = updateFunction(binding);
if(newBinding!=binding)
BindingOperations.SetBinding(visual, property, newBinding);
}
}

为了说明这是如何工作的,下面是您如何编写一个方法来替换所有 RelativeSource FindAncestor 实例中的特定 AncestorType,如下所示:

static void ReplaceFindAncestorType(Visual visual, Type fromType, Type toType)
{
UpdateBindings(visual, binding =>
binding.RelativeSource.Mode != RelativeSourceMode.FindAncestor ? binding :
binding.RelativeSource.AncestorType != fromType ? binding :
new Binding
{
RelativeSource = new RelativeSource(
RelativeSourceMode.FindAncestor,
toType,
binding.RelativeSource.AncestorLevel),
Path = binding.Path,
Mode = binding.Mode,
Converter = binding.Converter,
StringFormat = binding.StringFormat,
UpdateSourceTrigger = binding.UpdateSourceTrigger,
});
}

请注意,只有常用属性会被复制到新绑定(bind)中。

ReplaceFindAncestorVisualType 方法可以像这样使用:

elementHost.LayoutUpdated += (obj, e) =>
{
ReplaceFindAncestorType(elementHost, typeof(Window), typeof(ElementHost);
};

在您的情况下,这种通用替换技术将不起作用:它将在您的 ElementHost 上寻找一个不存在的 IsActive 属性。因此,您可能需要更改的不仅仅是 RelativeSource。这意味着您的实际代码将更像这样:

elementHost.LayoutUpdated += (obj, e) =>
{
UpdateBindings(elementHost, binding =>
binding.RelativeSource.AncestorType != typeof(Window) ? binding :
new Binding
{
Source = ultimateContainingWindowOrOtherObjectHavingIsActiveProperty,
Path = new PropertyPath("IsActive"), // Put property name here
});
};

请注意,上面的代码假定任何 FindAncestor:Window 绑定(bind)都是我们正在寻找的绑定(bind)。条件中可以根据需要添加更多条件。

替代方案

还有另一种完全不同的解决方案可用:实际上可以在无边框窗口中承载内容并添加自定义代码以保持此窗口位于 ElementHost 之上,因此它看起来位于另一个窗口内。这比听起来更棘手,因为您必须处理诸如 ActiveWindow、ForegroundWindow、Z 顺序、最小化状态、键盘焦点等内容。但如果您的需求非常简单,这可能是一个合理的解决方案。

关于wpf - 拦截 RelativeSource FindAncestor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2402280/

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