我得到了我要离开的 Pivot 项目的索引,而不是我要去的 Pivot 项目。
我已经搜索了一段时间,可以想到一些解决方案,比如在 View 中使用事件,然后使用 MVVM-Light 向 ViewModel 发送消息。但我宁愿不这样做,我宁愿坚持当前的实现,当然略有不同。
任何帮助表示赞赏
我的xml:
<controls:Pivot x:Name="ContentPivot" Margin="12,0,12,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command ="{Binding SelectSlideCommand}"
CommandParameter="{Binding SelectedIndex, ElementName=ContentPivot}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<controls:PivotItem x:Name="PivotItem0" Header="0">
<Image Source="{Binding ViewingSlides[0].Path}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</controls:PivotItem>
<controls:PivotItem x:Name="PivotItem1" Header="1">
<Image Source="{Binding ViewingSlides[1].Path}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</controls:PivotItem>
<controls:PivotItem x:Name="PivotItem2" Header="2">
<Image Source="{Binding ViewingSlides[2].Path}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</controls:PivotItem>
</controls:Pivot>
和 c#:
public RelayCommand<int> SelectSlideCommand;
SelectSlideCommand = new RelayCommand<int>((pivotItem) => SelectSlideAction(pivotItem));
void SelectSlideAction(int parameter)
{
currentIndex = parameter;
UpdateViewingSlides();
Debug.WriteLine("SelectSlideAction: " + parameter);
}
最佳答案
您的控件中是否有`SelectedItem 属性...您可以在您的 ViewModel 中将其连接到 TwoWay 绑定(bind)模式中的属性以始终获取更新的值(然后您将不需要此命令)...您也可以尝试
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command ="{Binding SelectSlideCommand}"
CommandParameter="{Binding SelectedItem, ElementName=ContentPivot}" />
</i:EventTrigger>
</i:Interaction.Triggers>
关于wpf - 枢轴控制 MVVM-Light-EventToCommand SelectedIndex 发送上一个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8809636/