【问题标题】:Pivot control MVVM-Light-EventToCommand SelectedIndex sends previous index枢轴控制 MVVM-Light-EventToCommand SelectedIndex 发送上一个索引
【发布时间】:2012-02-07 05:45:51
【问题描述】:

我得到了我要离开的 Pivot 项目的索引,而不是我要去的 Pivot 项目的索引。
我已经搜索了一段时间,可以想到一些解决方案,比如在视图中使用事件,然后使用 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);
    }

【问题讨论】:

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


    【解决方案1】:

    您的控件中是否有`SelectedItem 属性...您可以在您的 ViewModel 中将其连接到 TwoWay 绑定模式中的属性以始终获取更新的值(然后您将不需要此命令)...。你也可以试试

            <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <cmd:EventToCommand Command ="{Binding SelectSlideCommand}"
                                        CommandParameter="{Binding SelectedItem, ElementName=ContentPivot}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    

    【讨论】:

    • 谢谢!我已经尝试绑定 SelectedItem 属性,但我不知道 TwoWay 绑定模式。现在一切正常!谢谢
    • @RaymenScholten 使用牵引方式绑定,您可以从 ViewModel 中选择要在视图中显示的项目,反之亦然。它在这个场景中非常方便..
    猜你喜欢
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2011-12-23
    • 2015-09-26
    • 2013-02-25
    相关资源
    最近更新 更多