本文介绍了C#/ WPF:绑定到视觉/逻辑树外部的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用扩展WPF工具包中的DropDownButton控件。在其中,我托管了一个ListBox,在更改所选元素后,该ListBox应该隐藏包含的DropDownButton。

I'm using the DropDownButton control from the Extended WPF Toolkit. Inside of that, I'm hosting a ListBox which, upon change of selected element, should hide the containing DropDownButton.

我最初的做法是结合微软的交互性框架是:

My initial approach to do so in conjunction with Microsoft's interactivity framework was:

<xctk:DropDownButton x:Name="WorkspaceSelectorContainer" Content="This is the active workspace">
                    <xctk:DropDownButton.DropDownContent>
                            <ListBox ItemsSource="{Binding workspaces}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <ie:ChangePropertyAction PropertyName="IsOpen" Value="False" TargetObject="{Binding ElementName=WorkspaceSelectorContainer}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </ListBox>
                        </StackPanel>
                    </xctk:DropDownButton.DropDownContent>
                </xctk:DropDownButton>

但这没用,因为找不到WorkspaceSelectorContainer。
我没有找到ElementName的绑定,而是尝试查找DropDownButton类型的祖先,但这也不起作用。跟踪绑定表明,它仅解析祖先,直到DropDownButton.DropDownContent的最外层元素为止,而没有任何其他元素。例如

But this didn't work as WorkspaceSelectorContainer isn't found. Instead of binding by ElementName, I tried finding the ancestor of type DropDownButton, but this did not work either; tracing the binding showed it only resolved ancestors up to the outer most element of DropDownButton.DropDownContent, but not any further; e.g. the DropDownButton itself wasn't part of the ancestor tree.

好,所以我的下一个方法是

Ok, so my next approach was

<ie:ChangePropertyAction PropertyName="IsOpen" Value="False" TargetObject="{Binding Source={x:Reference WorkspaceSelectorContainer}}"/>

但由于抛出异常而无法正常工作

but that did not work either as it threw an Exception

A first chance exception of type 'System.Xaml.XamlObjectWriterException' occurred in System.Xaml.dll

Additional information: Cannot call MarkupExtension.ProvideValue because of a cyclical dependency. Properties inside a MarkupExtension cannot reference objects that reference the result of the MarkupExtension. 

不知道还能尝试什么。有谁知道如何解决这个问题?

Don't know what else to try anymore. Does anyone have an idea how to solve this?

是否有可能以某种方式引用包含所有内容的我的根网格,并从那里搜索DropDownButton元素?
诸如{Binding Source = {x:Reference MyRootGrid},ElementName = WorkspaceSelectorContainer}之类的东西(由于我无法合并source和ElementName,因此无法正常工作)

Would it be possible to somehow reference my root grid that contains everything, and search for the DropDownButton element from there?Something like {Binding Source={x:Reference MyRootGrid}, ElementName=WorkspaceSelectorContainer} (this doesn't work as I can't combine source and ElementName)

谢谢!

推荐答案

我有同样的问题(尽管在Silverlight中)。
通过引入一个资源对象解决了该问题,该资源对象具有一个对DropDownButton的引用,并且可以很容易地从DropDownContent内部进行绑定:

Well I had the same problem (albeit in Silverlight).Solved it by introducing a resource object that has a reference to the DropDownButton and that can easily be bound from within the DropDownContent:

<Foo.Resources>
    <BindableObjectReference
      x:Key="WorkspaceSelectorContainerReference"
      Object="{Binding ElementName=WorkspaceSelectorContainer}"/>
</Foo.Resources>
<DropDownButton x:Name="WorkspaceSelectorContainer" ...>
    <DropDownButton.DropDownContent>
        ...
        <ChangePropertyAction
            PropertyName="IsOpen"
            Value="False"
            TargetObject="{Binding Path=Object,
                Source={StaticResource WorkspaceSelectorContainerReference}}"/>
        ...
    </DropDownButton.DropDownContent>
</DropDownButton>

...以及魔术对象

public class BindableObjectReference : DependencyObject
{
    public object Object
    {
        get { return GetValue( ObjectProperty ); }
        set { SetValue( ObjectProperty, value ); }
    }

    public static readonly DependencyProperty ObjectProperty =
        DependencyProperty.Register( "Object", typeof( object ),
        typeof( BindableObjectReference ), new PropertyMetadata( null ) );
}

这篇关于C#/ WPF:绑定到视觉/逻辑树外部的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 14:43