RelativeSource绑定无法找到Frame祖先

时间:2015-01-12 11:35:03

标签: c# wpf xaml binding navigation

我正在尝试将CommandParameter作为Frame的实际Command对象传递到<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>

XAML

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

C#

NullReferenceException

运行时,会引发frame,表示{{1}}为空。 上面的代码可能有什么问题?

1 个答案:

答案 0 :(得分: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>