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

标签 .net wpf binding mvvm

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

相关文章:

.net - EF SaveChanges 不调用 Validate (IValidatableObject)

wpf - 初始化后如何激活/停用模块的 View ?

c# - Lambda 表达式搜索

c# - 基于绑定(bind)数据的 MenuItem 模板更改

c# - 按用户管理的职位查询和排序文档的最佳解决方案?

.net - 如何使用 WCF 的 ChannelFactory<T> 实现客户端异步调用?

c# - Asp.net MVC 中的部分 View 有什么意义

.net - 如何最好地处理 WPF 单选按钮?

wpf - 在wpf中绑定(bind)颜色的R G B属性

ios - 如何为 Xamarin.iOS 创建绑定(bind)?