我正在使用来自扩展WPF工具包的DropDownButton控件。在其中,我托管了一个ListBox,当选择元素更改时,应该隐藏包含的DropDownButton。
我最初的做法是与Microsoft的交互框架一起使用:
<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。 我尝试通过查找DropDownButton类型的祖先元素来进行绑定,但这也不起作用。跟踪绑定显示它只解析了DropDownButton.DropDownContent的最外层元素的祖先,而没有进一步解析;例如DropDownButton本身不是祖先树的一部分。
好的,那么我的下一个尝试是:
<ie:ChangePropertyAction PropertyName="IsOpen" Value="False" TargetObject="{Binding Source={x:Reference WorkspaceSelectorContainer}}"/>
但是这也不起作用,因为它抛出了一个异常。
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.
我已经尝试了各种方法,但仍无法解决问题。请问有没有人有什么好的想法可以解决这个问题?
是否可能以某种方式引用包含所有内容的根网格,并从中搜索DropDownButton元素? 类似于 {Binding Source={x:Reference MyRootGrid}, ElementName=WorkspaceSelectorContainer} (由于不能组合source和ElementName,因此这样做是不起作用的)
谢谢!