我有一个 SplitButton
在我的usercontrol
, usercontrol
的数据上下文是 view
它定义了我想要的命令 splitbutton
绑定(bind)到。
正如我在下面显示的简短 xaml 代码,第一个绑定(bind)有效,但第二个(button
中的 DropDownContent
)与输出无关:
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl....
或(如果我使用
ElementName
而不是 ReleativeSource
替换绑定(bind)表达式)Cannot find source for binding with reference 'ElementName=uc'...
<UserControl x:Name="uc"
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
>
<Grid>
<ItemsControl ItemsSource="{Binding ItemList, IsAsync=True}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<extToolkit:SplitButton Command="{Binding
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},
Path=DataContext.OpenCommand,
Mode=OneWay}"
CommandParameter="{Binding}"
Content="{Binding ID}">
<extToolkit:SplitButton.DropDownContent>
<Button Command="{Binding
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},
Path=DataContext.OpenCommand,
Mode=OneWay}"
CommandParameter="{Binding}"
Content="{Binding ID}"/>
</extToolkit:SplitButton.DropDownContent>
</extToolkit:SplitButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
最佳答案
最后我发现了
the
DropDownContent
and it content in different visual tree than the one that holds theSplitButton
. This way the Binding to RelativeSource cannot work, it does't find the desired relative source since the they are in different visual trees.
REF (虽然它是另一个控件,但我认为它们是基于同一件事)
所以我必须将我的虚拟机作为一个属性添加到项目对象中,并从那里绑定(bind)虚拟机的命令。
关于.net - 来自 SplitButton DropDownContent 的绑定(bind)命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8882842/