2017-04-23 69 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已經考慮了去附加語義。

雖然應該有更多的異常細節。