public void When_SingleSelectedItem_Event() { var panel = new StackPanel(); var item = new Border { Name = "b1" }; var SUT = new ListViewBase() { Style = null, Template = new ControlTemplate(() => new ItemsPresenter()), ItemsPanel = new ItemsPanelTemplate(() => panel), ItemContainerStyle = BuildBasicContainerStyle(), Items = { item } }; var model = new MyModel { SelectedItem = (object)null }; SUT.SetBinding( Selector.SelectedItemProperty, new Binding() { Path = "SelectedItem", Source = model, Mode = BindingMode.TwoWay } ); SUT.ApplyTemplate(); // Search on the panel for now, as the name lookup is not properly // aligned on net46. Assert.IsNotNull(panel.FindName("b1")); var selectionChanged = 0; SUT.SelectionChanged += (s, e) => { selectionChanged++; Assert.AreEqual(item, SUT.SelectedItem); // In windows, when programmatically changed, the bindings are updated *after* // the event is raised, but *before* when the SelectedItem is changed from the UI. Assert.IsNull(model.SelectedItem); }; SUT.SelectedIndex = 0; Assert.AreEqual(item, model.SelectedItem); Assert.IsNotNull(SUT.SelectedItem); Assert.AreEqual(1, selectionChanged); Assert.AreEqual(1, SUT.SelectedItems.Count); }
public void When_MultiSelectedItem() { var panel = new StackPanel(); var SUT = new ListViewBase() { Style = null, Template = new ControlTemplate(() => new ItemsPresenter()), ItemContainerStyle = BuildBasicContainerStyle(), ItemsPanel = new ItemsPanelTemplate(() => panel), Items = { new Border { Name = "b1" }, new Border { Name = "b2" } } }; SUT.ApplyTemplate(); // Search on the panel for now, as the name lookup is not properly // aligned on net46. Assert.IsNotNull(panel.FindName("b1")); Assert.IsNotNull(panel.FindName("b2")); SUT.SelectionMode = ListViewSelectionMode.Multiple; SUT.OnItemClicked(0); Assert.AreEqual(1, SUT.SelectedItems.Count); SUT.OnItemClicked(1); Assert.AreEqual(2, SUT.SelectedItems.Count); }