2017-04-23 27 views
0

我试图用鼠标重新订购listView项目,当我开始拖动或放弃项目时,我得到这个神秘的Parameter is not valid ArgumentException。没有其他细节,没有堆栈跟踪。崩溃整个应用程序。ArgumentException重新排序listViewItems

它时,我重新排序ObservableCollection<string>但不断拍击ObservableCollection<MyControl>

MyControl工作正常,只是一个简单的UserControlTextBlock内。我试过CollectionViewSource的方法和它是一样的。

任何想法?

MainPage.xaml中

<Page 
    x:Class="App3.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App3" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <ListView ItemsSource="{Binding Items}" CanReorderItems="True" AllowDrop="True"/> 
</Page> 

MainPage.xaml.cs中

using Windows.UI.Xaml.Controls; 
namespace App3 
{ 
    public sealed partial class MainPage : Page 
    { 
     private MainPageViewModel mainPageViewModel; 

     public MainPage() 
     { 
      this.InitializeComponent(); 

      mainPageViewModel = new MainPageViewModel(); 

      DataContext = mainPageViewModel; 

      // THIS IS NOT WORKING 
      MyControl myControl1 = new MyControl("Hello1"); 
      MyControl myControl2 = new MyControl("Hello2"); 
      MyControl myControl3 = new MyControl("Hello3"); 

      mainPageViewModel.Items.Add(myControl1); 
      mainPageViewModel.Items.Add(myControl2); 
      mainPageViewModel.Items.Add(myControl3); 

      // THIS IS WORKING 
      //string s1 = "h1"; 
      //string s2 = "h2"; 
      //string s3 = "h3"; 

      //mainPageViewModel.Items.Add(s1); 
      //mainPageViewModel.Items.Add(s2); 
      //mainPageViewModel.Items.Add(s3); 
     } 
    } 
} 

MainPageViewModel.cs

using System.Collections.ObjectModel; 

namespace App3 
{ 
    public class MainPageViewModel : BaseModel 
    { 
     // THIS IS NOT WORKING 
     private ObservableCollection<MyControl> items = new ObservableCollection<MyControl>(); 
     public ObservableCollection<MyControl> Items 
     { 
      get { return items; } 
      set 
      { 
       items = value; 
       OnPropertyChanged(); 
      } 
     } 

     // THIS IS WORKING 
     //private ObservableCollection<string> items = new ObservableCollection<string>(); 
     //public ObservableCollection<string> Items 
     //{ 
     // get { return items; } 
     // set 
     // { 
     //  items = value; 
     //  OnPropertyChanged(); 
     // } 
     //} 
    } 
} 

MyControl.xaml

<UserControl 
    x:Class="App3.MyControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App3" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400"> 

    <TextBlock Name="tb"/> 
</UserControl> 

MyControl.cs

using Windows.UI.Xaml.Controls; 
namespace App3 
{ 
    public sealed partial class MyControl : UserControl 
    { 
     public MyControl(string sName) 
     { 
      this.InitializeComponent(); 
      tb.Text = sName; 
     } 
    } 
} 

BaseModel.cs

using System.ComponentModel; 
using System.Runtime.CompilerServices; 

namespace App3 
{ 
    public class BaseModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged([CallerMemberName]string name = "") 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(name)); 
      } 
     } 
    } 
} 

Ë DIT

看起来像重新排序简单对象也没有问题,所以我想在重新排序UserControl时出现了问题。

namespace App3 
{ 
    public class SampleClass 
    { 
     private string sName; 
     public SampleClass(string sName) 
     { 
      this.sName = sName; 
     } 
     public override string ToString() 
     { 
      return sName; 
     } 
    } 
} 

EDIT从CheckBox崩溃酷似MyControl派生2

重新排序对象。

using Windows.UI.Xaml.Controls; 

namespace App3 
{ 
    public class MyCheckBox : CheckBox 
    { 
     public MyCheckBox(string sName) 
     { 
      Content = sName; 
     } 
    } 
} 

同样为标准复选框

CheckBox checkBox1 = new CheckBox() { Content = "hello1" }; 
CheckBox checkBox2 = new CheckBox() { Content = "hello2" }; 
CheckBox checkBox3 = new CheckBox() { Content = "hello3" }; 

mainPageViewModel.Items.Add(checkBox1); 
mainPageViewModel.Items.Add(checkBox2); 
mainPageViewModel.Items.Add(checkBox3); 

EDIT 3

好像唯一的解决办法是使用简单的类用于保持数据

using Windows.UI.Xaml.Controls; 

namespace App3 
{ 
    public class MyCheckBox : BaseModel 
    { 
     private string sName; 
     public string Name 
     { 
      get { return sName; } 
      set 
      { 
       sName = value; 
       OnPropertyChanged(); 
      } 
     } 

     public MyCheckBox(string sName) 
     { 
      Name = sName; 
     } 
    } 
} 

DataTemplateListViewItem以显示它

<Page 
    x:Class="App3.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App3" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <ListView ItemsSource="{Binding Items}" CanReorderItems="True" AllowDrop="True"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <CheckBox Content="{Binding Name}"/> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</Page> 

,而不是直接加入到UserControlListView

回答

1

它看起来像Visuals的重新排序不完全支持。当在一个列表视图中重新组织项目时,下面会发生很多事情,例如动态创建和销毁容纳内容的容器等。

为了显示诉诸元素,需要在视觉树中的其他地方添加一些项目。 拖放会尝试附加可能导致异常的已连接Visual(您的UserControl) - Visual不能附加到多个父项。

与数据模板一起使用POCOs/ViewModels的方法应该足以完成工作,因为ListView已经考虑了去附加语义。

虽然应该有更多的异常细节。