RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1275289
Accepted
North Face
North Face
Asked:2022-04-27 10:26:01 +0000 UTC2022-04-27 10:26:01 +0000 UTC 2022-04-27 10:26:01 +0000 UTC

列表框 | 选择列表项

  • 772

有一个带有图像的列表框。用户用鼠标在ListBox中选择一张图片,将键盘上的箭头在列表中向右移动(From Image,当前元素的Source用于增加)。在当前实现中,不设置为当前索引的位置。使用当前正确的索引,它将选择从移动的图像中设置为 +1,如果您将其设置为 hard ContainerFromIndex(3),它还会以相同的方式将其设置为 +1,在另一个方向上设置为 -1。怎么看不到0,在+1和-1之间。

ListBoxPhotos.Items.Insert(Index + 2, img);
ListBoxPhotos.Items.RemoveAt(Index);
ListBoxPhotos.SelectedItem = null;
ListBoxPhotos.SelectedIndex = Index + 1;
ListBoxItem listBoxItem = ListBoxPhotos.ItemContainerGenerator.ContainerFromIndex(Index) as ListBoxItem;
listBoxItem.Focus();

<ListBox 
    SelectionChanged="ListBoxItemClick" 
    SelectionMode="Single" 
    x:Name="ListBoxPhotos"  
    ScrollViewer.VerticalScrollBarVisibility="Disabled" 
    VerticalAlignment="Center" 
    Background="#FF5CB57A" 
    BorderBrush="{x:Null}" KeyDown="StackPanel_KeyDown">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" KeyDown="StackPanel_KeyDown"/>
            </ItemsPanelTemplate>
         </ListBox.ItemsPanel>
</ListBox>

这个选项做它需要做的,它只是显示一个MessageBox,这里不需要。每次显示并且焦点更改或某些属性时。我无法弄清楚哪个属性正在改变。如果已定义,则可以在没有 MessageBox 的情况下以编程方式调用它。

for (int i = 0; i < ListBoxPhotos.Items.Count; i++)
{
    object yourObject = ListBoxPhotos.Items[i];
    ListBoxItem lbi = (ListBoxItem)ListBoxPhotos.ItemContainerGenerator.ContainerFromItem(yourObject);

    if (lbi.IsFocused)
    {
        MessageBox.Show("Item at index " + i.ToString() + " has the focus.");
        break;
    }
    ListBoxPhotos.SelectedIndex = Index + 1;
    ListBoxItem listBoxItem = ListBoxPhotos.ItemContainerGenerator.ContainerFromIndex(ListBoxPhotos.SelectedIndex - 1) as ListBoxItem;
    listBoxItem.Focus();

}
c#
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    aepot
    2022-04-27T18:54:28Z2022-04-27T18:54:28Z

    单击按钮时移动集合元素并不像看起来那么困难。

    我将基于集合ObservableCollection和数据绑定做一个例子。据我了解,您不熟悉 MVVM 设计模式,并且您拥有窗口类中的所有代码。因此,我将在那里向您展示数据上下文设置。

    下面的代码将需要一个帮助类来使用命令(它们在 WPF 中使用而不是事件处理程序)。

    public class RelayCommand : ICommand
    {
        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;
    
        public event EventHandler CanExecuteChanged
        {
            add => CommandManager.RequerySuggested += value;
            remove => CommandManager.RequerySuggested -= value;
        }
    
        public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
            => (_execute, _canExecute) = (execute, canExecute);
    
        public bool CanExecute(object parameter)
            => _canExecute == null || _canExecute(parameter);
    
        public void Execute(object parameter)
            => _execute(parameter);
    }
    

    和一个小清单

    public enum Direction
    {
        Backward,
        Forward
    }
    

    窗口类需要这样修改

    public sealed partial class MainWindow : Window, INotifyPropertyChanged
    {
        private ObservableCollection<string> _items;
        private ICommand _moveCommand;
        private string _selectedItem;
    
        public ObservableCollection<string> Items
        {
            get => _items;
            set
            {
                _items = value;
                OnPropertyChanged();
            }
        }
    
        public int SelectedIndex => Items.IndexOf(SelectedItem);
    
        public string SelectedItem
        {
            get => _selectedItem;
            set
            {
                _selectedItem = value;
                OnPropertyChanged();
            }
        }
    
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
    
        public ICommand MoveCommand => _moveCommand ?? (_moveCommand = new RelayCommand(parameter =>
        {
            if (Enum.TryParse(parameter as string, out Direction direction))
            {
                int index = SelectedIndex;
                if (direction == Direction.Backward && index > 0)
                {
                    Items.Move(index, index - 1);
                }
                else if (direction == Direction.Forward && index < Items.Count - 1)
                {
                    Items.Move(index, index + 1);
                }
            }
        }));
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Items = new ObservableCollection<string>
            {
                "https://image1_url",
                "https://image2_url"
            };
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    

    这里:

    • 接口实现INotifyPropertyChanged,使接口可以动态更新。
    • ObservableCollection<string>- 带有来自的图像路径的集合Source。
    • SelectedItem, 选中的列表框元素附在此处
    • SelectedIndex转换SelectedItem为其索引(假设集合中的所有字符串都是唯一的且不重复)
    • MoveCommand- 按下键时调用的命令

    这是标记

    <ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
        <ListBox.InputBindings>
            <KeyBinding Key="Right" Command="{Binding MoveCommand}" CommandParameter="Forward"/>
            <KeyBinding Key="Left" Command="{Binding MoveCommand}" CommandParameter="Backward"/>
        </ListBox.InputBindings>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
    

    当您按下键盘上的箭头时,元素会按预期向右或向左移动。焦点仍然在选定的元素上。

    可以使用修饰符指定键InputBindings,例如Key="Right" Modifiers="Ctrl".

    请注意,控件甚至没有名称x:Name,这里不需要它们。

    • 2
  2. North Face
    2022-04-27T18:38:50Z2022-04-27T18:38:50Z

    重点如下。它ListBox具有在列表中左右滚动的标准行为。它需要重新加载。我没有,并且发生了冲突。目前,您可以像这样解决它:

    1. 让 mainGrid定义一个KeyDown名为OnKeyDownHandler.
    private void OnKeyDownHandler(object sender, KeyEventArgs e)
    
    1. 在这个方法中,执行算法:
     if (e.Key == Key.Right)
        {
            if (ListBoxPhotos.Items.Count > 0)
            {
                var Path = (ListBoxPhotos.SelectedItem as Image).Source.ToString();
                int Index = ListBoxPhotos.SelectedIndex;
                Image img = common.PathStringToImage(Path, (ListBoxPhotos.Items.Count));
    
                if (Index != ListBoxPhotos.Items.Count - 1)
                {
                    ListBoxPhotos.Items.Insert(Index + 2, img);
                    ListBoxPhotos.Items.RemoveAt(Index);
    
                    var item = (ListBoxItem)ListBoxPhotos.ItemContainerGenerator.ContainerFromIndex(Index + 1);
                    item?.Focus();
                }
            }
        }
    

    最终代码:

    private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Right)
        {
            if (ListBoxPhotos.Items.Count > 0)
            {
                var Path = (ListBoxPhotos.SelectedItem as Image).Source.ToString();
                int Index = ListBoxPhotos.SelectedIndex;
                Image img = common.PathStringToImage(Path, (ListBoxPhotos.Items.Count));
    
                if (Index != ListBoxPhotos.Items.Count - 1)
                {
                    ListBoxPhotos.Items.Insert(Index + 2, img);
                    ListBoxPhotos.Items.RemoveAt(Index);
    
                    var item = (ListBoxItem)ListBoxPhotos.ItemContainerGenerator.ContainerFromIndex(Index + 1);
                    item?.Focus();
                }
            }
        }
    }
    

    链接到另一个答案

    • 1

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5