2015-01-12 79 views
0

我試圖通過爲CommandParameter實際Frame對象到我施加Command的RelativeSource綁定找不到框架祖先

XAML

<Frame NavigationUIVisibility="Hidden" Source="{Binding TargetContentPage}"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="ContentRendered"> 
        <command:EventToCommand Command="{Binding ContentRendered}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Frame}}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
</Frame> 

C#

public RelayCommand<Frame> ContentRendered 
{ 
    get 
    { 
      return new RelayCommand<Frame>(frame => frame.RemoveBackEntry()); 
    } 
} 

運行時,一個NullReferenceException被提出說frame爲空。 上面的代碼可能有什麼錯誤?

+0

您是否可能缺少Mode = FindAncestor? – Lennart

+0

@Clemens是的,你是對的,我錯過了,請添加它作爲答案請。 – AymenDaoudi

回答

3

Frame和EventToCommand之間沒有祖先/後代關係。改爲使用ElementName綁定:

<Frame x:Name="frame" ...> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="ContentRendered"> 
      <command:EventToCommand 
       Command="{Binding ContentRendered}" 
       CommandParameter="{Binding ElementName=frame}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</Frame>