2010-07-26 46 views
1

我在我的WPF应用程序中有一个动作Edit,它绑定到ListView控件中的项目,即它在项目被双击或工具栏中的Edit按钮时执行被点击。这个动作反过来显示一个模式窗口与编辑的东西。WPF:显示对话窗口后选中的ListView项目

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

我的代码没有使用任何花式的ListView样式或什么不是,那么可能是什么原因造成这种情况? 我可以张贴根据要求我的代码,但它是非常标准的

编辑:

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

+0

你如何编辑项目?替换它们还是只更改属性?代码中有些事情会导致您正在遇到的行为。 – 2010-07-27 13:02:18

+1

我用新项目替换它们。我已经想通了,不知何故使列表的SelectedItems属性无效,即背景在选定的项目上保持蓝色,但由于新项目在技术上没有选择(不在列表中),所以它们不被视为这样。我通过在SelectedItems集合中添加新对象来解决问题。 – 2010-07-27 13:12:35

回答

0

代码中的某些内容必定会导致此问题。以下是一个与预期相符的示例。

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; } 
    } 
} 
+0

你是对的,看我的编辑。 – 2010-07-26 18:35:46

0

你有什么设置ListView.SelectionMode?它听起来像是设置为Multiple(点击一个项目扩展了选择),而您可能想将其设置为Extended(点击某个项目并按下Control或Shift时,选择会延长)。

虽然我不知道该怎么说编辑命令问题。也许SelectedIndex和多个选择有一个奇怪的行为 - 可能检查ListView.SelectedItems集合中的对象的计数呢?

+0

SelectionMode是Extented - 只要我不显示任何模式对话框,它就会按预期工作。并且SelectedIndex等于选择多个项目时的第一个选定项目,因为编辑按钮的可见性正确对应于选择项目(即使其中的一些)。 – 2010-07-26 14:25:49