2012-01-16 129 views
2

我在usercontrol一個SplitButton,則usercontrol的DataContext的是view定義命令我希望splitbutton結合。綁定命令不工作

正如我簡短的XAML代碼顯示如下,第一綁定工作,但第二(在DropDownContentbutton)不與輸出:

無法爲參照結合找到源「的RelativeSource FindAncestor ,AncestorType ='System.Windows.Controls.UserControl ....

或(如果我頂替結合使用的ReleativeSourceElementName代替表達)

無法爲參照 '的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> 

回答

2

最後,我發現了

DropDownContent和不同的視覺樹 它的內容比擁有SplitButton的那個。這樣綁定到 RelativeSource不能工作,它沒有找到所需的相對源 ,因爲它們在不同的視覺樹中。

REF(雖然它是另一種控制,但我認爲它們都是基於同樣的事情)

所以,我有我的VM添加爲項目對象的屬性,並從那裏結合虛擬機的命令。

0

我認爲你應該參考你的命令Command="{x:Static someNamespace:SomeClass.OpenCommand}"