CommandParameter 总是传递带有 ElementName 绑定的 EventArgs,没有实际的绑定值
CommandParameter always passes EventArgs with ElementName binding, no actual binded value
因此,我想使用 Behavior SDK 将 Pivot 事件绑定到我的视图模型。绑定如下所示:
<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>
在视图模型中执行的操作如下所示:
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 中,但不会在共享项目中找到它。
我猜这是因为枢轴项目没有 Windows 8 控件。
现在我只想传递 SelectedItem
,而不是 EventArgs。我将 CommandParameter
更改为以下内容:
<Core:InvokeCommandAction Command="{Binding LoadAdditionalData}"
CommandParameter="{Binding ElementName=pvtMain, Path=SelectedItem}" />
仍然,该值仍然是 PivotItemEventArgs。我错过了什么吗?
经过一番努力,我偶然发现了 'solution'。
我在 PivotItemLoading 事件上绑定了命令。不知何故,在第一次加载事件触发时,参数始终为 PivotItemEventArgs
.
类型
然而,在第二次加载后,正确的对象(SelectedItem
)被传递了!
我不知道确切的原因,但很高兴知道这有效。
因此,我想使用 Behavior SDK 将 Pivot 事件绑定到我的视图模型。绑定如下所示:
<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>
在视图模型中执行的操作如下所示:
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 中,但不会在共享项目中找到它。
我猜这是因为枢轴项目没有 Windows 8 控件。
现在我只想传递 SelectedItem
,而不是 EventArgs。我将 CommandParameter
更改为以下内容:
<Core:InvokeCommandAction Command="{Binding LoadAdditionalData}"
CommandParameter="{Binding ElementName=pvtMain, Path=SelectedItem}" />
仍然,该值仍然是 PivotItemEventArgs。我错过了什么吗?
经过一番努力,我偶然发现了 'solution'。
我在 PivotItemLoading 事件上绑定了命令。不知何故,在第一次加载事件触发时,参数始终为 PivotItemEventArgs
.
然而,在第二次加载后,正确的对象(SelectedItem
)被传递了!
我不知道确切的原因,但很高兴知道这有效。