| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599 | 
							- using System;
 
- using System.Collections;
 
- using System.Collections.Generic;
 
- using System.Collections.ObjectModel;
 
- using System.Linq;
 
- using Avalonia.Collections;
 
- using Avalonia.Controls.Presenters;
 
- using Avalonia.Controls.Primitives;
 
- using Avalonia.Controls.Selection;
 
- using Avalonia.Controls.Templates;
 
- using Avalonia.Data;
 
- using Avalonia.Input;
 
- using Avalonia.Input.Platform;
 
- using Avalonia.Interactivity;
 
- using Avalonia.UnitTests;
 
- using Moq;
 
- using Xunit;
 
- namespace Avalonia.Controls.UnitTests.Primitives
 
- {
 
-     public class SelectingItemsControlTests_Multiple
 
-     {
 
-         private MouseTestHelper _helper = new MouseTestHelper();
 
-         [Fact]
 
-         public void Setting_SelectedIndex_Should_Add_To_SelectedItems()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedIndex = 1;
 
-             Assert.Equal(new[] { "bar" }, target.SelectedItems.Cast<object>().ToList());
 
-         }
 
-         [Fact]
 
-         public void Adding_SelectedItems_Should_Set_SelectedIndex()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems.Add("bar");
 
-             Assert.Equal(1, target.SelectedIndex);
 
-         }
 
-         [Fact]
 
-         public void Assigning_Single_SelectedItems_Should_Set_SelectedIndex()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             ((Control)target.Presenter).ApplyTemplate();
 
-             target.SelectedItems = new AvaloniaList<object>("bar");
 
-             Assert.Equal(1, target.SelectedIndex);
 
-             Assert.Equal(new[] { "bar" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 1 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Assigning_Multiple_SelectedItems_Should_Set_SelectedIndex()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedItems = new AvaloniaList<string>("foo", "bar", "baz");
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal(new[] { "foo", "bar", "baz" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Selected_Items_Should_Be_Marked_When_Panel_Created_After_SelectedItems_Is_Set()
 
-         {
 
-             // Issue #2565.
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems = new AvaloniaList<string>("foo", "bar", "baz");
 
-             target.Presenter.ApplyTemplate();
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal(new[] { "foo", "bar", "baz" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Reassigning_SelectedItems_Should_Clear_Selection()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems.Add("bar");
 
-             target.SelectedItems = new AvaloniaList<object>();
 
-             Assert.Equal(-1, target.SelectedIndex);
 
-             Assert.Null(target.SelectedItem);
 
-         }
 
-         [Fact]
 
-         public void Adding_First_SelectedItem_Should_Raise_SelectedIndex_SelectedItem_Changed()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             bool indexRaised = false;
 
-             bool itemRaised = false;
 
-             target.PropertyChanged += (s, e) =>
 
-             {
 
-                 indexRaised |= e.Property.Name == "SelectedIndex" &&
 
-                     (int)e.OldValue == -1 &&
 
-                     (int)e.NewValue == 1;
 
-                 itemRaised |= e.Property.Name == "SelectedItem" &&
 
-                     (string)e.OldValue == null &&
 
-                     (string)e.NewValue == "bar";
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems.Add("bar");
 
-             Assert.True(indexRaised);
 
-             Assert.True(itemRaised);
 
-         }
 
-         [Fact]
 
-         public void Adding_Subsequent_SelectedItems_Should_Not_Raise_SelectedIndex_SelectedItem_Changed()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems.Add("foo");
 
-             bool raised = false;
 
-             target.PropertyChanged += (s, e) => 
 
-                 raised |= e.Property.Name == "SelectedIndex" ||
 
-                           e.Property.Name == "SelectedItem";
 
-             target.SelectedItems.Add("bar");
 
-             Assert.False(raised);
 
-         }
 
-         [Fact]
 
-         public void Removing_Last_SelectedItem_Should_Raise_SelectedIndex_Changed()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems.Add("foo");
 
-             bool raised = false;
 
-             target.PropertyChanged += (s, e) => 
 
-                 raised |= e.Property.Name == "SelectedIndex" && 
 
-                           (int)e.OldValue == 0 && 
 
-                           (int)e.NewValue == -1;
 
-             target.SelectedItems.RemoveAt(0);
 
-             Assert.True(raised);
 
-         }
 
-         [Fact]
 
-         public void Adding_SelectedItems_Should_Set_Item_IsSelected()
 
-         {
 
-             var items = new[]
 
-             {
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-             };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedItems.Add(items[0]);
 
-             target.SelectedItems.Add(items[1]);
 
-             var foo = target.Presenter.Panel.Children[0];
 
-             Assert.True(items[0].IsSelected);
 
-             Assert.True(items[1].IsSelected);
 
-             Assert.False(items[2].IsSelected);
 
-         }
 
-         [Fact]
 
-         public void Assigning_SelectedItems_Should_Set_Item_IsSelected()
 
-         {
 
-             var items = new[]
 
-             {
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-             };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedItems = new AvaloniaList<object> { items[0], items[1] };
 
-             Assert.True(items[0].IsSelected);
 
-             Assert.True(items[1].IsSelected);
 
-             Assert.False(items[2].IsSelected);
 
-         }
 
-         [Fact]
 
-         public void Removing_SelectedItems_Should_Clear_Item_IsSelected()
 
-         {
 
-             var items = new[]
 
-             {
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-             };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedItems.Add(items[0]);
 
-             target.SelectedItems.Add(items[1]);
 
-             target.SelectedItems.Remove(items[1]);
 
-             Assert.True(items[0].IsSelected);
 
-             Assert.False(items[1].IsSelected);
 
-         }
 
-         [Fact]
 
-         public void Reassigning_SelectedItems_Should_Clear_Item_IsSelected()
 
-         {
 
-             var items = new[]
 
-             {
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-             };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems.Add(items[0]);
 
-             target.SelectedItems.Add(items[1]);
 
-             target.SelectedItems = new AvaloniaList<object> { items[0], items[1] };
 
-             Assert.False(items[0].IsSelected);
 
-             Assert.False(items[1].IsSelected);
 
-         }
 
-         [Fact]
 
-         public void Setting_SelectedIndex_Should_Unmark_Previously_Selected_Containers()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedItems.Add("foo");
 
-             target.SelectedItems.Add("bar");
 
-             Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
 
-             target.SelectedIndex = 2;
 
-             Assert.Equal(new[] { 2 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Range_Select_Should_Select_Range()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[]
 
-                 {
 
-                     "foo",
 
-                     "bar",
 
-                     "baz",
 
-                     "qux",
 
-                     "qiz",
 
-                     "lol",
 
-                 },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedIndex = 1;
 
-             target.SelectRange(3);
 
-             Assert.Equal(new[] { "bar", "baz", "qux" }, target.SelectedItems.Cast<object>().ToList());
 
-         }
 
-         [Fact]
 
-         public void Range_Select_Backwards_Should_Select_Range()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[]
 
-                 {
 
-                     "foo",
 
-                     "bar",
 
-                     "baz",
 
-                     "qux",
 
-                     "qiz",
 
-                     "lol",
 
-                 },
 
-                 SelectionMode = SelectionMode.Multiple,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedIndex = 3;
 
-             target.SelectRange(1);
 
-             Assert.Equal(new[] { "qux", "bar", "baz" }, target.SelectedItems.Cast<object>().ToList());
 
-         }
 
-         [Fact]
 
-         public void Second_Range_Select_Backwards_Should_Select_From_Original_Selection()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[]
 
-                 {
 
-                     "foo",
 
-                     "bar",
 
-                     "baz",
 
-                     "qux",
 
-                     "qiz",
 
-                     "lol",
 
-                 },
 
-                 SelectionMode = SelectionMode.Multiple,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedIndex = 2;
 
-             target.SelectRange(5);
 
-             target.SelectRange(4);
 
-             Assert.Equal(new[] { "baz", "qux", "qiz" }, target.SelectedItems.Cast<object>().ToList());
 
-         }
 
-         [Fact]
 
-         public void Setting_SelectedIndex_After_Range_Should_Unmark_Previously_Selected_Containers()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz", "qux" },
 
-                 Template = Template(),
 
-                 SelectedIndex = 0,
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectRange(2);
 
-             Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
 
-             target.SelectedIndex = 3;
 
-             Assert.Equal(new[] { 3 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Toggling_Selection_After_Range_Should_Work()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz", "foo", "bar", "baz" },
 
-                 Template = Template(),
 
-                 SelectedIndex = 0,
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectRange(3);
 
-             Assert.Equal(new[] { 0, 1, 2, 3 }, SelectedContainers(target));
 
-             target.Toggle(4);
 
-             Assert.Equal(new[] { 0, 1, 2, 3, 4 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Suprious_SelectedIndex_Changes_Should_Not_Be_Triggered()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             var selectedIndexes = new List<int>();
 
-             target.GetObservable(TestSelector.SelectedIndexProperty).Subscribe(x => selectedIndexes.Add(x));
 
-             target.SelectedItems = new AvaloniaList<object> { "bar", "baz" };
 
-             target.SelectedItem = "foo";
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal(new[] { -1, 1, 0 }, selectedIndexes);
 
-         }
 
-         [Fact]
 
-         public void Can_Set_SelectedIndex_To_Another_Selected_Item()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedItems.Add("foo");
 
-             target.SelectedItems.Add("bar");
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal(new[] { "foo", "bar" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
 
-             var raised = false;
 
-             target.SelectionChanged += (s, e) =>
 
-             {
 
-                 raised = true;
 
-                 Assert.Empty(e.AddedItems);
 
-                 Assert.Equal(new[] { "foo" }, e.RemovedItems);
 
-             };
 
-             target.SelectedIndex = 1;
 
-             Assert.True(raised);
 
-             Assert.Equal(1, target.SelectedIndex);
 
-             Assert.Equal(new[] { "bar" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 1 }, SelectedContainers(target));
 
-         }
 
-         /// <summary>
 
-         /// Tests a problem discovered with ListBox with selection.
 
-         /// </summary>
 
-         /// <remarks>
 
-         /// - Items is bound to DataContext first, followed by say SelectedIndex
 
-         /// - When the ListBox is removed from the visual tree, DataContext becomes null (as it's
 
-         ///   inherited)
 
-         /// - This changes Items to null, which changes SelectedIndex to null as there are no
 
-         ///   longer any items
 
-         /// - However, the news that DataContext is now null hasn't yet reached the SelectedItems
 
-         ///   binding and so the unselection is sent back to the ViewModel
 
-         /// 
 
-         /// This is a similar problem to that tested by XamlBindingTest.Should_Not_Write_To_Old_DataContext.
 
-         /// However, that tests a general property binding problem: here we are writing directly
 
-         /// to the SelectedItems collection - not via a binding - so it's something that the 
 
-         /// binding system cannot solve. Instead we solve it by not clearing SelectedItems when
 
-         /// DataContext is in the process of changing.
 
-         /// </remarks>
 
-         [Fact]
 
-         public void Should_Not_Write_SelectedItems_To_Old_DataContext()
 
-         {
 
-             var vm = new OldDataContextViewModel();
 
-             var target = new TestSelector();
 
-             var itemsBinding = new Binding
 
-             {
 
-                 Path = "Items",
 
-                 Mode = BindingMode.OneWay,
 
-             };
 
-             var selectedItemsBinding = new Binding
 
-             {
 
-                 Path = "SelectedItems",
 
-                 Mode = BindingMode.OneWay,
 
-             };
 
-             // Bind Items and SelectedItems to the VM.
 
-             target.Bind(TestSelector.ItemsProperty, itemsBinding);
 
-             target.Bind(TestSelector.SelectedItemsProperty, selectedItemsBinding);
 
-             // Set DataContext and SelectedIndex
 
-             target.DataContext = vm;
 
-             target.SelectedIndex = 1;
 
-             // Make sure SelectedItems are written back to VM.
 
-             Assert.Equal(new[] { "bar" }, vm.SelectedItems);
 
-             // Clear DataContext and ensure that SelectedItems is still set in the VM.
 
-             target.DataContext = null;
 
-             Assert.Equal(new[] { "bar" }, vm.SelectedItems);
 
-             // Ensure target's SelectedItems is now clear.
 
-             Assert.Empty(target.SelectedItems);
 
-         }
 
-         /// <summary>
 
-         /// See <see cref="Should_Not_Write_SelectedItems_To_Old_DataContext"/>.
 
-         /// </summary>
 
-         [Fact]
 
-         public void Should_Not_Write_SelectionModel_To_Old_DataContext()
 
-         {
 
-             var vm = new OldDataContextViewModel();
 
-             var target = new TestSelector();
 
-             var itemsBinding = new Binding
 
-             {
 
-                 Path = "Items",
 
-                 Mode = BindingMode.OneWay,
 
-             };
 
-             var selectionBinding = new Binding
 
-             {
 
-                 Path = "Selection",
 
-                 Mode = BindingMode.OneWay,
 
-             };
 
-             // Bind Items and Selection to the VM.
 
-             target.Bind(TestSelector.ItemsProperty, itemsBinding);
 
-             target.Bind(TestSelector.SelectionProperty, selectionBinding);
 
-             // Set DataContext and SelectedIndex
 
-             target.DataContext = vm;
 
-             target.SelectedIndex = 1;
 
-             // Make sure selection is written to selection model
 
-             Assert.Equal(1, vm.Selection.SelectedIndex);
 
-             // Clear DataContext and ensure that selection is still set in model.
 
-             target.DataContext = null;
 
-             Assert.Equal(1, vm.Selection.SelectedIndex);
 
-             // Ensure target's SelectedItems is now clear.
 
-             Assert.Empty(target.SelectedItems);
 
-         }
 
-         [Fact]
 
-         public void Unbound_SelectedItems_Should_Be_Cleared_When_DataContext_Cleared()
 
-         {
 
-             var data = new
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz" },
 
-             };
 
-             var target = new TestSelector
 
-             {
 
-                 DataContext = data,
 
-                 Template = Template(),
 
-             };
 
-             var itemsBinding = new Binding { Path = "Items" };
 
-             target.Bind(TestSelector.ItemsProperty, itemsBinding);
 
-             Assert.Same(data.Items, target.Items);
 
-             target.SelectedItems.Add("bar");
 
-             target.DataContext = null;
 
-             Assert.Empty(target.SelectedItems);
 
-         }
 
-         [Fact]
 
-         public void Adding_To_SelectedItems_Should_Raise_SelectionChanged()
 
-         {
 
-             var items = new[] { "foo", "bar", "baz" };
 
-             var target = new TestSelector
 
-             {
 
-                 DataContext = items,
 
-                 Template = Template(),
 
-                 Items = items,
 
-             };
 
-             var called = false;
 
-             target.SelectionChanged += (s, e) =>
 
-             {
 
-                 Assert.Equal(new[] { "bar" }, e.AddedItems.Cast<object>().ToList());
 
-                 Assert.Empty(e.RemovedItems);
 
-                 called = true;
 
-             };
 
-             target.SelectedItems.Add("bar");
 
-             Assert.True(called);
 
-         }
 
-         [Fact]
 
-         public void Removing_From_SelectedItems_Should_Raise_SelectionChanged()
 
-         {
 
-             var items = new[] { "foo", "bar", "baz" };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-                 SelectedItem = "bar",
 
-             };
 
-             var called = false;
 
-             target.SelectionChanged += (s, e) =>
 
-             {
 
-                 Assert.Equal(new[] { "bar" }, e.RemovedItems.Cast<object>().ToList());
 
-                 Assert.Empty(e.AddedItems);
 
-                 called = true;
 
-             };
 
-             target.SelectedItems.Remove("bar");
 
-             Assert.True(called);
 
-         }
 
-         [Fact]
 
-         public void Assigning_SelectedItems_Should_Raise_SelectionChanged()
 
-         {
 
-             var items = new[] { "foo", "bar", "baz" };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-                 SelectedItem = "bar",
 
-             };
 
-             var called = false;
 
-             target.SelectionChanged += (s, e) =>
 
-             {
 
-                 Assert.Equal(new[] { "foo", "baz" }, e.AddedItems.Cast<object>());
 
-                 Assert.Equal(new[] { "bar" }, e.RemovedItems.Cast<object>());
 
-                 called = true;
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedItems = new AvaloniaList<object>("foo", "baz");
 
-             Assert.True(called);
 
-         }
 
-         
 
-         [Fact]
 
-         public void Shift_Selecting_From_No_Selection_Selects_From_Start()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Shift);
 
-                 var panel = target.Presenter.Panel;
 
-                 Assert.Equal(new[] { "Foo", "Bar", "Baz" }, target.SelectedItems);
 
-                 Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Ctrl_Selecting_Raises_SelectionChanged_Events()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz", "Qux" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 SelectionChangedEventArgs receivedArgs = null;
 
-                 target.SelectionChanged += (_, args) => receivedArgs = args;
 
-                 void VerifyAdded(string selection)
 
-                 {
 
-                     Assert.NotNull(receivedArgs);
 
-                     Assert.Equal(new[] { selection }, receivedArgs.AddedItems);
 
-                     Assert.Empty(receivedArgs.RemovedItems);
 
-                 }
 
-                 void VerifyRemoved(string selection)
 
-                 {
 
-                     Assert.NotNull(receivedArgs);
 
-                     Assert.Equal(new[] { selection }, receivedArgs.RemovedItems);
 
-                     Assert.Empty(receivedArgs.AddedItems);
 
-                 }
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
 
-                 VerifyAdded("Bar");
 
-                 receivedArgs = null;
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
 
-                 VerifyAdded("Baz");
 
-                 receivedArgs = null;
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Control);
 
-                 VerifyAdded("Qux");
 
-                 receivedArgs = null;
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Control);
 
-                 VerifyRemoved("Bar");
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Ctrl_Selecting_SelectedItem_With_Multiple_Selection_Active_Sets_SelectedItem_To_Next_Selection()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz", "Qux" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(1, target.SelectedIndex);
 
-                 Assert.Equal("Bar", target.SelectedItem);
 
-                 Assert.Equal(new[] { "Bar", "Baz", "Qux" }, target.SelectedItems);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(2, target.SelectedIndex);
 
-                 Assert.Equal("Baz", target.SelectedItem);
 
-                 Assert.Equal(new[] { "Baz", "Qux" }, target.SelectedItems);
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Ctrl_Selecting_Non_SelectedItem_With_Multiple_Selection_Active_Leaves_SelectedItem_The_Same()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(1, target.SelectedIndex);
 
-                 Assert.Equal("Bar", target.SelectedItem);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(1, target.SelectedIndex);
 
-                 Assert.Equal("Bar", target.SelectedItem);
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Should_Ctrl_Select_Correct_Item_When_Duplicate_Items_Are_Present()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[3]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[4], modifiers: KeyModifiers.Control);
 
-                 var panel = target.Presenter.Panel;
 
-                 Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
 
-                 Assert.Equal(new[] { 3, 4 }, SelectedContainers(target));
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Should_Shift_Select_Correct_Item_When_Duplicates_Are_Present()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[3]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[5], modifiers: KeyModifiers.Shift);
 
-                 var panel = target.Presenter.Panel;
 
-                 Assert.Equal(new[] { "Foo", "Bar", "Baz" }, target.SelectedItems);
 
-                 Assert.Equal(new[] { 3, 4, 5 }, SelectedContainers(target));
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Can_Shift_Select_All_Items_When_Duplicates_Are_Present()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[5], modifiers: KeyModifiers.Shift);
 
-                 var panel = target.Presenter.Panel;
 
-                 Assert.Equal(new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" }, target.SelectedItems);
 
-                 Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, SelectedContainers(target));
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Shift_Selecting_Raises_SelectionChanged_Events()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz", "Qux" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 SelectionChangedEventArgs receivedArgs = null;
 
-                 target.SelectionChanged += (_, args) => receivedArgs = args;
 
-                 void VerifyAdded(params string[] selection)
 
-                 {
 
-                     Assert.NotNull(receivedArgs);
 
-                     Assert.Equal(selection, receivedArgs.AddedItems);
 
-                     Assert.Empty(receivedArgs.RemovedItems);
 
-                 }
 
-                 void VerifyRemoved(string selection)
 
-                 {
 
-                     Assert.NotNull(receivedArgs);
 
-                     Assert.Equal(new[] { selection }, receivedArgs.RemovedItems);
 
-                     Assert.Empty(receivedArgs.AddedItems);
 
-                 }
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
 
-                 VerifyAdded("Bar");
 
-                 receivedArgs = null;
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Shift);
 
-                 VerifyAdded("Baz", "Qux");
 
-                 receivedArgs = null;
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Shift);
 
-                 VerifyRemoved("Qux");
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Duplicate_Items_Are_Added_To_SelectedItems_In_Order()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
 
-                 Assert.Equal(new[] { "Foo" }, target.SelectedItems);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[4], modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(new[] { "Foo", "Bar", "Foo" }, target.SelectedItems);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(new[] { "Foo", "Bar", "Foo", "Bar" }, target.SelectedItems);
 
-             }
 
-         }
 
-         [Fact]
 
-         public void SelectAll_Sets_SelectedIndex_And_SelectedItem()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Template = Template(),
 
-                 Items = new[] { "Foo", "Bar", "Baz" },
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectAll();
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal("Foo", target.SelectedItem);
 
-         }
 
-         [Fact]
 
-         public void SelectAll_Raises_SelectionChanged_Event()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Template = Template(),
 
-                 Items = new[] { "Foo", "Bar", "Baz" },
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             SelectionChangedEventArgs receivedArgs = null;
 
-             target.SelectionChanged += (_, args) => receivedArgs = args;
 
-             target.SelectAll();
 
-             Assert.NotNull(receivedArgs);
 
-             Assert.Equal(target.Items, receivedArgs.AddedItems);
 
-             Assert.Empty(receivedArgs.RemovedItems);
 
-         }
 
-         [Fact]
 
-         public void UnselectAll_Clears_SelectedIndex_And_SelectedItem()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Template = Template(),
 
-                 Items = new[] { "Foo", "Bar", "Baz" },
 
-                 SelectionMode = SelectionMode.Multiple,
 
-                 SelectedIndex = 0,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.UnselectAll();
 
-             Assert.Equal(-1, target.SelectedIndex);
 
-             Assert.Equal(null, target.SelectedItem);
 
-         }
 
-         [Fact]
 
-         public void SelectAll_Handles_Duplicate_Items()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Template = Template(),
 
-                 Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectAll();
 
-             Assert.Equal(new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" }, target.SelectedItems);
 
-         }
 
-         [Fact]
 
-         public void Adding_Item_Before_SelectedItems_Should_Update_Selection()
 
-         {
 
-             var items = new ObservableCollection<string>
 
-             {
 
-                "Foo",
 
-                "Bar",
 
-                "Baz"
 
-             };
 
-             var target = new ListBox
 
-             {
 
-                 Template = Template(),
 
-                 Items = items,
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectAll();
 
-             items.Insert(0, "Qux");
 
-             Assert.Equal(1, target.SelectedIndex);
 
-             Assert.Equal("Foo", target.SelectedItem);
 
-             Assert.Equal(new[] { "Foo", "Bar", "Baz" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 1, 2, 3 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Removing_Item_Before_SelectedItem_Should_Update_Selection()
 
-         {
 
-             var items = new ObservableCollection<string>
 
-             {
 
-                "Foo",
 
-                "Bar",
 
-                "Baz"
 
-             };
 
-             var target = new TestSelector
 
-             {
 
-                 Template = Template(),
 
-                 Items = items,
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectedIndex = 1;
 
-             target.SelectRange(2);
 
-             Assert.Equal(new[] { "Bar", "Baz" }, target.SelectedItems);
 
-             items.RemoveAt(0);
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal("Bar", target.SelectedItem);
 
-             Assert.Equal(new[] { "Bar", "Baz" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Removing_SelectedItem_With_Multiple_Selection_Active_Should_Update_Selection()
 
-         {
 
-             var items = new ObservableCollection<string>
 
-             {
 
-                "Foo",
 
-                "Bar",
 
-                "Baz"
 
-             };
 
-             var target = new ListBox
 
-             {
 
-                 Template = Template(),
 
-                 Items = items,
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectAll();
 
-             items.RemoveAt(0);
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal("Bar", target.SelectedItem);
 
-             Assert.Equal(new[] { "Bar", "Baz" }, target.SelectedItems);
 
-             Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Replacing_Selected_Item_Should_Update_SelectedItems()
 
-         {
 
-             var items = new ObservableCollection<string>
 
-             {
 
-                "Foo",
 
-                "Bar",
 
-                "Baz"
 
-             };
 
-             var target = new ListBox
 
-             {
 
-                 Template = Template(),
 
-                 Items = items,
 
-                 SelectionMode = SelectionMode.Multiple,
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             target.SelectAll();
 
-             items[1] = "Qux";
 
-             Assert.Equal(new[] { "Foo", "Baz" }, target.SelectedItems);
 
-         }
 
-         [Fact]
 
-         public void Left_Click_On_SelectedItem_Should_Clear_Existing_Selection()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz" },
 
-                     ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 target.SelectAll();
 
-                 Assert.Equal(3, target.SelectedItems.Count);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
 
-                 Assert.Equal(1, target.SelectedItems.Count);
 
-                 Assert.Equal(new[] { "Foo", }, target.SelectedItems);
 
-                 Assert.Equal(new[] { 0 }, SelectedContainers(target));
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Right_Click_On_SelectedItem_Should_Not_Clear_Existing_Selection()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz" },
 
-                     ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 target.SelectAll();
 
-                 Assert.Equal(3, target.SelectedItems.Count);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[0], MouseButton.Right);
 
-                 Assert.Equal(3, target.SelectedItems.Count);
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Right_Click_On_UnselectedItem_Should_Clear_Existing_Selection()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz" },
 
-                     ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Shift);
 
-                 Assert.Equal(2, target.SelectedItems.Count);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], MouseButton.Right);
 
-                 Assert.Equal(1, target.SelectedItems.Count);
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Adding_Selected_ItemContainers_Should_Update_Selection()
 
-         {
 
-             var items = new AvaloniaList<ItemContainer>(new[]
 
-             {
 
-                 new ItemContainer(),
 
-                 new ItemContainer(),
 
-             });
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 SelectionMode = SelectionMode.Multiple,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             items.Add(new ItemContainer { IsSelected = true });
 
-             items.Add(new ItemContainer { IsSelected = true });
 
-             Assert.Equal(2, target.SelectedIndex);
 
-             Assert.Equal(items[2], target.SelectedItem);
 
-             Assert.Equal(new[] { items[2], items[3] }, target.SelectedItems);
 
-         }
 
-         [Fact]
 
-         public void Shift_Right_Click_Should_Not_Select_Multiple()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz" },
 
-                     ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], MouseButton.Right, modifiers: KeyModifiers.Shift);
 
-                 Assert.Equal(1, target.SelectedItems.Count);
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Ctrl_Right_Click_Should_Not_Select_Multiple()
 
-         {
 
-             using (UnitTestApplication.Start())
 
-             {
 
-                 var target = new ListBox
 
-                 {
 
-                     Template = Template(),
 
-                     Items = new[] { "Foo", "Bar", "Baz" },
 
-                     ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
 
-                     SelectionMode = SelectionMode.Multiple,
 
-                 };
 
-                 AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
 
-                 target.ApplyTemplate();
 
-                 target.Presenter.ApplyTemplate();
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
 
-                 _helper.Click((Interactive)target.Presenter.Panel.Children[2], MouseButton.Right, modifiers: KeyModifiers.Control);
 
-                 Assert.Equal(1, target.SelectedItems.Count);
 
-             }
 
-         }
 
-         [Fact]
 
-         public void Adding_To_Selection_Should_Set_SelectedIndex()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.SelectedItems.Add("bar");
 
-             Assert.Equal(1, target.SelectedIndex);
 
-         }
 
-         [Fact]
 
-         public void Assigning_Null_To_Selection_Should_Create_New_SelectionModel()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             var oldSelection = target.Selection;
 
-             target.Selection = null;
 
-             Assert.NotNull(target.Selection);
 
-             Assert.NotSame(oldSelection, target.Selection);
 
-         }
 
-         [Fact]
 
-         public void Assigning_SelectionModel_With_Different_Source_To_Selection_Should_Fail()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             var selection = new SelectionModel<string> { Source = new[] { "baz" } };
 
-             Assert.Throws<ArgumentException>(() => target.Selection = selection);
 
-         }
 
-         [Fact]
 
-         public void Assigning_SelectionModel_With_Null_Source_To_Selection_Should_Set_Source()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             var selection = new SelectionModel<string>();
 
-             target.Selection = selection;
 
-             Assert.Same(target.Items, selection.Source);
 
-         }
 
-         [Fact]
 
-         public void Assigning_Single_Selected_Item_To_Selection_Should_Set_SelectedIndex()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             var selection = new SelectionModel<string> { SingleSelect = false };
 
-             selection.Select(1);
 
-             target.Selection = selection;
 
-             Assert.Equal(1, target.SelectedIndex);
 
-             Assert.Equal(new[] { "bar" }, target.Selection.SelectedItems);
 
-             Assert.Equal(new[] { 1 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Assigning_Multiple_Selected_Items_To_Selection_Should_Set_SelectedIndex()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar", "baz" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             var selection = new SelectionModel<string> { SingleSelect = false };
 
-             selection.SelectRange(0, 2);
 
-             target.Selection = selection;
 
-             Assert.Equal(0, target.SelectedIndex);
 
-             Assert.Equal(new[] { "foo", "bar", "baz" }, target.Selection.SelectedItems);
 
-             Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
 
-         }
 
-         [Fact]
 
-         public void Reassigning_Selection_Should_Clear_Selection()
 
-         {
 
-             var target = new TestSelector
 
-             {
 
-                 Items = new[] { "foo", "bar" },
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Selection.Select(1);
 
-             target.Selection = new SelectionModel<string>();
 
-             Assert.Equal(-1, target.SelectedIndex);
 
-             Assert.Null(target.SelectedItem);
 
-         }
 
-         [Fact]
 
-         public void Assigning_Selection_Should_Set_Item_IsSelected()
 
-         {
 
-             var items = new[]
 
-             {
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-                 new ListBoxItem(),
 
-             };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             var selection = new SelectionModel<object> { SingleSelect = false };
 
-             selection.SelectRange(0, 1);
 
-             target.Selection = selection;
 
-             Assert.True(items[0].IsSelected);
 
-             Assert.True(items[1].IsSelected);
 
-             Assert.False(items[2].IsSelected);
 
-         }
 
-         [Fact]
 
-         public void Assigning_Selection_Should_Raise_SelectionChanged()
 
-         {
 
-             var items = new[] { "foo", "bar", "baz" };
 
-             var target = new TestSelector
 
-             {
 
-                 Items = items,
 
-                 Template = Template(),
 
-                 SelectedItem = "bar",
 
-             };
 
-             var raised = 0;
 
-             target.SelectionChanged += (s, e) =>
 
-             {
 
-                 if (raised == 0)
 
-                 {
 
-                     Assert.Empty(e.AddedItems.Cast<object>());
 
-                     Assert.Equal(new[] { "bar" }, e.RemovedItems.Cast<object>());
 
-                 }
 
-                 else
 
-                 {
 
-                     Assert.Equal(new[] { "foo", "baz" }, e.AddedItems.Cast<object>());
 
-                     Assert.Empty(e.RemovedItems.Cast<object>());
 
-                 }
 
-                 ++raised;
 
-             };
 
-             target.ApplyTemplate();
 
-             target.Presenter.ApplyTemplate();
 
-             var selection = new SelectionModel<string> { Source = items, SingleSelect = false };
 
-             selection.Select(0);
 
-             selection.Select(2);
 
-             target.Selection = selection;
 
-             Assert.Equal(2, raised);
 
-         }
 
-         private static IEnumerable<int> SelectedContainers(SelectingItemsControl target)
 
-         {
 
-             return target.Presenter.Panel.Children
 
-                 .Select((x, i) => x.Classes.Contains(":selected") ? i : -1)
 
-                 .Where(x => x != -1);
 
-         }
 
-         private static FuncControlTemplate Template()
 
-         {
 
-             return new FuncControlTemplate<SelectingItemsControl>((control, scope) =>
 
-                 new ItemsPresenter
 
-                 {
 
-                     Name = "PART_ItemsPresenter",
 
-                     [~ItemsPresenter.ItemsPanelProperty] = control[~ItemsControl.ItemsPanelProperty],
 
-                 }.RegisterInNameScope(scope));
 
-         }
 
-         private class TestSelector : SelectingItemsControl
 
-         {
 
-             public static readonly new AvaloniaProperty<IList> SelectedItemsProperty = 
 
-                 SelectingItemsControl.SelectedItemsProperty;
 
-             public static readonly new DirectProperty<SelectingItemsControl, ISelectionModel> SelectionProperty =
 
-                 SelectingItemsControl.SelectionProperty;
 
-             public TestSelector()
 
-             {
 
-                 SelectionMode = SelectionMode.Multiple;
 
-             }
 
-             public new IList SelectedItems
 
-             {
 
-                 get { return base.SelectedItems; }
 
-                 set { base.SelectedItems = value; }
 
-             }
 
-             public new ISelectionModel Selection
 
-             {
 
-                 get => base.Selection;
 
-                 set => base.Selection = value;
 
-             }
 
-             public new SelectionMode SelectionMode
 
-             {
 
-                 get { return base.SelectionMode; }
 
-                 set { base.SelectionMode = value; }
 
-             }
 
-             public void SelectAll() => Selection.SelectAll();
 
-             public void UnselectAll() => Selection.Clear();
 
-             public void SelectRange(int index) => UpdateSelection(index, true, true);
 
-             public void Toggle(int index) => UpdateSelection(index, true, false, true);
 
-         }
 
-         private class OldDataContextViewModel
 
-         {
 
-             public OldDataContextViewModel()
 
-             {
 
-                 Items = new List<string> { "foo", "bar" };
 
-                 SelectedItems = new List<string>();
 
-                 Selection = new SelectionModel<string>();
 
-             }
 
-             public List<string> Items { get; } 
 
-             public List<string> SelectedItems { get; }
 
-             public SelectionModel<string> Selection { get; }
 
-         }
 
-         private class ItemContainer : Control, ISelectable
 
-         {
 
-             public string Value { get; set; }
 
-             public bool IsSelected { get; set; }
 
-         }
 
-     }
 
- }
 
 
  |