c# - CommandParameter 始终通过 ElementName 绑定(bind)传递 EventArgs,没有实际绑定(bind)值

标签 c# mvvm windows-phone-8.1 behavior

因此,我想使用 Behavior SDK 将 Pivot 事件绑定(bind)到我的 View 模型。绑定(bind)看起来像这样:

<Pivot Grid.Row="1" x:Name="pvtMain"
       ItemsSource="{Binding Items}"
       HeaderTemplate="{StaticResource PivotHeaderTemplate}">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="PivotItemLoading">
            <Core:InvokeCommandAction Command="{Binding LoadAdditionalData}"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>

</Pivot>

在 Viewmodel 中执行的操作如下所示:
private void _CommandLoadAdditionalData(object parameter) {
    var test = (parameter as PivotItemEventArgs);      
}

问题如下:我收到错误:type or namespace name 'PivotItemEventArgs' could not be found (are you missing a using directive or an assembly reference?) .

但是当我运行该项目时,一切正常。深入挖掘后,PivotItemEventArgs 确实位于 Windows.UI.Xaml.Controls 中,但不会在 Shared 项目中找到。

我猜这是因为枢轴项不是 Windows 8 控件。

现在我只想要 SelectedItem被传递,而不是 EventArgs。我改变了我的CommandParameter到以下:
<Core:InvokeCommandAction Command="{Binding LoadAdditionalData}"
                                      CommandParameter="{Binding ElementName=pvtMain, Path=SelectedItem}" />

尽管如此,该值仍然是 PivotItemEventArgs。我错过了什么吗?

最佳答案

因此,经过一番挣扎,我偶然发现了“解决方案”。

我将命令绑定(bind)在 PivotItemLoading 事件上。不知何故,在第一次加载事件触发时,参数始终是 PivotItemEventArgs 类型。 .

然而,在第二次加载之后,正确的对象(SelectedItem)正在被传递!

我不知道确切的原因,但很高兴知道这是有效的。

关于c# - CommandParameter 始终通过 ElementName 绑定(bind)传递 EventArgs,没有实际绑定(bind)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995263/

相关文章:

c# - 这是使用MVVM设计模式创建新窗口的最佳方法

wpf - 从不同的 View 执行相同的命令 - MMVM

c# - 如何使用C#4.0 Win Form应用程序隐藏TreeView控件中某个TreeNode的复选框?

c# - 如何为 Visual Studio 2017 生成 odata v4 c# 代理客户端?

c# - 对于 WPF 窗口中介服务,使用简单注入(inject)器按键解析实例的替代方法是什么?

visual-studio-2013 - Windows.Storage 命名空间在可移植类库项目中不可见

c# - 如何在 c# windows phone 8.1 应用程序中写入 excel 文件?

c# - Windows Phone 8.1 - 在 gridview 上使用图片库图像进行数据虚拟化

Java 与 C# 毫秒时间戳生成

c# - 在 ASP.NET Core 中的每个操作之前查询数据库以获取角色授权