wpf - 枢轴控制 MVVM-Light-EventToCommand SelectedIndex 发送上一个索引

标签 wpf xaml windows-phone-7 mvvm mvvm-light

我得到了我要离开的 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/

相关文章:

wpf - 我还能如何在 View 模型的 View 中触发动画?

c# - 如何从父级访问嵌套的用户控件 View 模型

windows-phone-7 - 如何从前台应用程序切换到后台代理?

android - 对于使用移动应用程序的 Sorcery gem,我应该使用什么身份验证方法?

c# - 为什么所有的坐标和大小都很奇怪?

wpf - ListView:在资源字典中定义 ItemsPanelTemplate

c# - ComboBox 项目的自定义项目模板

xaml - Xamarin.Forms 与数据触发器绑定(bind) - 值不更新

wpf - 从 DataTemplateSelector 显式刷新 DataTemplate?

c# - 在开发 XNA 应用程序时如何避免频繁的 int/float/double 转换?