【问题标题】:WPF: selected ListView items after showing a dialog windowWPF:显示对话框窗口后选择的 ListView 项目
【发布时间】:2010-07-26 13:18:47
【问题描述】:

我的 WPF 应用程序中有一个动作 Edit,它绑定到 ListView 控件中的项目,即当双击项目或单击工具栏中的 Edit 按钮时执行。此操作依次显示带有编辑内容的模式窗口。

现在,当我在列表中选择多个项目时,单击“编辑”,这些项目在背景中保持选中状态,而且,当我关闭对话框时,它们仍然处于选中状态,因为它们的背景是蓝色的。但是,它们似乎没有被选中,因为工具栏中的“编辑”按钮被禁用(编辑操作的CanExecute 方法只检查FileList.SelectedIndex != -1。此外,当我单击时,“选定”项目不会被取消选择其他一些列表项 - 只有当我明确地一一点击它们时它们才会被取消选择 - 就好像蓝色背景卡在它们上面一样。

我的代码没有使用任何花哨的ListView 样式或其他样式,那么可能是什么原因造成的? 我可以根据要求发布我的代码,但这非常标准。

编辑:

在削减我的代码后,我终于找到了导致这个问题的原因。显示对话框后,我编辑数据绑定集合中的项目,以便更新 ListView(即将绑定对象替换为新对象)。问题是,为什么这会导致问题,我应该如何解决?

【问题讨论】:

  • 如何编辑项目?替换它们或只是更改属性?您的代码中发生的某些事情会导致您遇到的行为。
  • 我将它们替换为新项目。我发现这会以某种方式使列表的 SelectedItems 属性无效,即所选项目的背景保持为蓝色,但由于技术上未选择新项目(不在列表中),因此它们不会被视为这样。我通过在 SelectedItems 集合中添加新对象解决了这个问题。

标签: wpf listview


【解决方案1】:

您的代码中的某些内容一定会导致此问题。下面是一个行为符合预期的示例。

XAML:

<Window x:Class="TestDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <RoutedUICommand x:Key="EditItemsCommand" Text="Edit Items" />
    </Window.Resources>

    <Window.CommandBindings>
        <CommandBinding 
            Command="{StaticResource EditItemsCommand}" 
            CanExecute="EditItems_CanExecute" 
            Executed="EditItems_Executed" />
    </Window.CommandBindings>

    <StackPanel>
        <Button Name="_editButton" Content="Edit" Command="{StaticResource EditItemsCommand}" />
        <Button Content="Unselect all" Click="OnUnselectAll" />
        <ListView 
            Name="_listView"
            ItemsSource="{Binding Path=Items}" 
            SelectionMode="Extended"
            MouseDoubleClick="OnListViewMouseDoubleClick">
        </ListView>
    </StackPanel>
</Window>

后面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace TestDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        public IEnumerable<string> Items
        {
            get 
            {
                for (int i = 0; i < 10; i++) { yield return i.ToString(); }
            }
        }

        private void EditItems_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _listView != null && _listView.SelectedItems.Count > 0;
        }

        private void EditItems_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            EditWindow editWindow = new EditWindow();
            editWindow.EditItems = _listView.SelectedItems.Cast<string>();
            editWindow.ShowDialog();
        }

        private void OnListViewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            _editButton.Command.Execute(null);
        }

        private void OnUnselectAll(object sender, RoutedEventArgs e)
        {
            _listView.SelectedItem = null;
        }
    }
}

XAML:

<Window x:Class="TestDemo.EditWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EditWindow">
    <ListBox ItemsSource="{Binding Path=EditItems}" />
</Window>

后面的代码:

using System;
using System.Collections.Generic;
using System.Windows;

namespace TestDemo
{
    public partial class EditWindow : Window
    {
        public EditWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        public IEnumerable<string> EditItems { get; set; }
    }
}

【讨论】:

    【解决方案2】:

    您将ListView.SelectionMode 设置为什么?听起来它设置为Multiple(单击项目扩展选择),而您可能希望将其设置为Extended(单击项目并按Control 或Shift 时扩展选择)。

    不过,对于编辑命令问题,我不知道该说些什么。 SelectedIndex 和多选可能存在奇怪的行为 - 可能改为检查 ListView.SelectedItems 集合中的对象数?

    【讨论】:

    • SelectionMode 已扩展 - 只要我不显示任何模式对话框,它就会按预期工作。并且 SelectedIndex 等于选择多个项目时的第一个选定项目,因为“编辑”按钮的可见性正确地对应于选择项目(即使是其中的几个)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多