.net SplitButton DropDownContent中的绑定命令不起作用
在我的.net SplitButton DropDownContent中的绑定命令不起作用,.net,wpf,binding,mvvm,.net,Wpf,Binding,Mvvm,在我的usercontrol中有一个SplitButton,usercontrol的数据上下文是一个视图,它定义了我想要将SplitButton绑定到的命令 正如我下面简短的xaml代码所示,第一个绑定可以工作,但是第二个(DropDownContent中的按钮)与输出不匹配: 找不到引用为“RelativeSource”的绑定源 FindAncestor,AncestorType='System.Windows.Controls.UserControl 或者(如果我使用ElementName而
usercontrol
中有一个SplitButton
,usercontrol
的数据上下文是一个视图
,它定义了我想要将SplitButton
绑定到的命令
正如我下面简短的xaml代码所示,第一个绑定可以工作,但是第二个(DropDownContent
中的按钮)与输出不匹配:
找不到引用为“RelativeSource”的绑定源
FindAncestor,AncestorType='System.Windows.Controls.UserControl
或者(如果我使用ElementName
而不是relativesource
替换绑定表达式)
找不到引用为“ElementName=uc”的绑定源
最后我发现了
DropDownContent
和不同视觉树中的it内容
而不是按住拆分按钮的按钮。这样就可以将绑定到
RelativeSource无法工作,它找不到所需的相对源
因为它们在不同的视觉树中
(虽然这是另一个控件,但我认为它们基于相同的东西)
因此,我必须将我的VM作为属性添加到item对象中,并从中绑定VM的命令。我认为您应该将您的命令引用为command=“{x:Static someNamespace:SomeClass.OpenCommand}”
<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>