列表框中 select 下一个/上一个项目的按钮命令

Button command(s) to select next / previous item in ListBox

是否有一个简单的命令我可以使用按钮将其发送到标准 WPF 列表框,使其到达列表中的 select 下一个/上一个项目?

目前我正在使用这个解决方案:

            <Button Width="30" Height="30" x:Name="PreviousButton">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <ei:ChangePropertyAction Increment="True"
                                                 PropertyName="SelectedIndex"
                                                 TargetName="MyListBox"
                                                 Value="-1" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
           <!-- ListBox would be here. -->
           <Button Width="30" Height="30" x:Name="NextButton">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <ei:ChangePropertyAction Increment="True"
                                                 PropertyName="SelectedIndex"
                                                 TargetName="MyListBox"
                                                 Value="1" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>

这很好。如果您在列表中的第一项(与最后一项相同)时点击上一个按钮,它确实会导致异常,但当前计划是告诉它禁用按钮,如果 selectedindex 是first/last 项在列表中。

我问这个问题只是想看看我是否错过了一个绝招。如果是这样的话,"No, that's not possible, you have to do it this other way" 是一个完全可以接受的答案。

它没有 "standard" 命令,只需创建将更改 int value; 的 NextItemCommand 和 PrevItemCommand,您可以绑定到 ListBox.SelectedIndex,这就是您想要的。关于 enable\disable 按钮只是为每个命令传递一个 Enabled 方法,因此它会检查是否可以更改 value

使用 mvvm 会很好而且更简单。这就是我将如何解决 it.Note 我的示例代码使用 mvvmLight 工具包。

查看

<Window x:Class="Whosebug1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Whosebug1"
        xmlns:viewModel="clr-namespace:Whosebug1.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <viewModel:MainViewModel x:Key="MainViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource MainViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <ListBox Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Students}" x:Name="ListBox"
                 SelectedIndex="{Binding SelectedIndex}"/>
        <StackPanel Orientation="Horizontal" Grid.Row="1">
            <Button Content="Next" Width="50" Height="24" Command="{Binding NextCommand}" CommandParameter="{Binding ElementName=ListBox,Path=SelectedIndex}"/>
            <Button Content="Previous" Width="50" Height="24" Command="{Binding PreviousCommand}" CommandParameter="{Binding ElementName=ListBox,Path=SelectedIndex}"/>
        </StackPanel>
    </Grid>
</Window>

视图模型

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;

namespace Whosebug1.ViewModel
{

    public class MainViewModel : ViewModelBase
    {

        public MainViewModel()
        {
         NextCommand=new RelayCommand<int>(OnNext,CanNext);
         PreviousCommand=new RelayCommand<int>(OnPrevious,CanPrevious);
            SelectedIndex = 0;

            foreach (var student in GetStudent())
            {
                _students.Add(student);
            }
        }

        public ICommand NextCommand { get; set; }
        public ICommand PreviousCommand { get; set; }
        private int _selectedIndex;


        private List<Student> GetStudent()
        {
            return new List<Student>()
            {
                new Student {Id = 0,Name = "Kwame0"},
                 new Student {Id = 0,Name = "Kwame1"},
                 new Student {Id = 0,Name = "Kwame2"},
                 new Student {Id = 0,Name = "Kwame3"},
                  new Student {Id = 0,Name = "Kwame4"},
                 new Student {Id = 0,Name = "Kwame5"},
                 new Student {Id = 0,Name = "Kwame6"},
                 new Student {Id = 0,Name = "Kwame7"},
                   new Student {Id = 0,Name = "Kwame8"},
                 new Student {Id = 0,Name = "Kwame9"},
                 new Student {Id = 0,Name = "Kwame10"},
                 new Student {Id = 0,Name = "Kwame11"},
                  new Student {Id = 0,Name = "Kwame12"},
                 new Student {Id = 0,Name = "Kwame13"},
                 new Student {Id = 0,Name = "Kwame14"},
                 new Student {Id = 0,Name = "Kwame15"},
            };
        }
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value;RaisePropertyChanged(()=>SelectedIndex); }
        }


        private ObservableCollection<Student> _students=new ObservableCollection<Student>();

        public ObservableCollection<Student> Students
        {
            get { return _students; }
            set { _students = value; }
        }


        private void OnNext(int index)
        {
            if (SelectedIndex != Students.Count)
                SelectedIndex += 1;
            else
            {
                SelectedIndex = 0;
            }
        }

        private bool CanNext(int indext)
        {
            return SelectedIndex != Students.Count;
        }

        private void OnPrevious(int index)
        {
            if (SelectedIndex != 0)
                SelectedIndex -= 1;
            else
            {
                SelectedIndex = Students.Count;
            }
        }

        private bool CanPrevious(int index)
        {
            return SelectedIndex != 0;
        }

    }

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return $"{Id}-{Name}";
        }
    }
}