gpt4 book ai didi

.net - 来自 SplitButton DropDownContent 的绑定(bind)命令不起作用

转载 作者:行者123 更新时间:2023-12-03 10:16:48 24 4
gpt4 key购买 nike

我有一个 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 the SplitButton. 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/

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