示例#1
0
        public void DataContexts_Should_Be_Correctly_Set()
        {
            var items = new object[]
            {
                "Foo",
                new Item("Bar"),
                new TextBlock { Text = "Baz" },
                new ListBoxItem { Content = "Qux" },
            };

            var target = new ListBox
            {
                Template = new FuncControlTemplate(CreateListBoxTemplate),
                DataContext = "Base",
                DataTemplates = new DataTemplates
                {
                    new FuncDataTemplate<Item>(x => new Button { Content = x })
                },
                Items = items,
            };

            target.ApplyTemplate();

            var dataContexts = target.Presenter.Panel.Children
                .Cast<Control>()
                .Select(x => x.DataContext)
                .ToList();

            Assert.Equal(
                new object[] { items[0], items[1], "Base", "Base" },
                dataContexts);
        }
示例#2
0
        public void LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items()
        {
            var target = new ListBox
            {
                Template = new FuncControlTemplate(CreateListBoxTemplate),
                Items = new[] { "Foo", "Bar", "Baz " },
            };

            target.ApplyTemplate();

            Assert.Equal(3, target.GetLogicalChildren().Count());

            foreach (var child in target.GetLogicalChildren())
            {
                Assert.IsType<ListBoxItem>(child);
            }
        }
示例#3
0
        public void Focusing_Item_With_Arrow_Key_Should_Select_It()
        {
            var target = new ListBox
            {
                Template = new ControlTemplate(CreateListBoxTemplate),
                Items = new[] { "Foo", "Bar", "Baz " },
            };

            target.ApplyTemplate();

            target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
            {
                RoutedEvent = InputElement.GotFocusEvent,
                NavigationMethod = NavigationMethod.Directional,
            });

            Assert.Equal(0, target.SelectedIndex);
        }
示例#4
0
        public void Clicking_Item_Should_Select_It()
        {
            var target = new ListBox
            {
                Template = new ControlTemplate(CreateListBoxTemplate),
                Items = new[] { "Foo", "Bar", "Baz " },
            };

            target.ApplyTemplate();

            target.Presenter.Panel.Children[0].RaiseEvent(new PointerPressEventArgs
            {
                RoutedEvent = InputElement.PointerPressedEvent,
                MouseButton = MouseButton.Left,
            });

            Assert.Equal(0, target.SelectedIndex);
        }
示例#5
0
        public void Ctrl_Selecting_Non_SelectedItem_With_Multiple_Selection_Active_Leaves_SelectedItem_The_Same()
        {
            var target = new ListBox
            {
                Template      = Template(),
                Items         = new[] { "Foo", "Bar", "Baz" },
                SelectionMode = SelectionMode.Multiple,
            };

            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();
            _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
            _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: InputModifiers.Control);

            Assert.Equal(1, target.SelectedIndex);
            Assert.Equal("Bar", target.SelectedItem);

            _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: InputModifiers.Control);

            Assert.Equal(1, target.SelectedIndex);
            Assert.Equal("Bar", target.SelectedItem);
        }
示例#6
0
        public void Right_Click_On_SelectedItem_Should_Not_Clear_Existing_Selection()
        {
            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,
            };

            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);
        }
示例#7
0
        public void ListBoxItem_Containers_Should_Be_Generated()
        {
            var items = new[] { "Foo", "Bar", "Baz " };
            var target = new ListBox
            {
                Template = new FuncControlTemplate(CreateListBoxTemplate),
                Items = items,
            };

            target.ApplyTemplate();

            var text = target.Presenter.Panel.Children
                .OfType<ListBoxItem>()
                .Do(x => x.Template = ListBoxItemTemplate())
                .Do(x => x.ApplyTemplate())
                .Select(x => x.Presenter.Child)
                .OfType<TextBlock>()
                .Select(x => x.Text)
                .ToList();

            Assert.Equal(items, text);
        }
示例#8
0
        public void Setting_SelectedItem_Should_Set_Panel_Keyboard_Navigation()
        {
            var target = new ListBox
            {
                Template = new FuncControlTemplate(CreateListBoxTemplate),
                Items    = new[] { "Foo", "Bar", "Baz " },
            };

            target.ApplyTemplate();

            target.Presenter.Panel.Children[1].RaiseEvent(new PointerPressEventArgs
            {
                RoutedEvent = InputElement.PointerPressedEvent,
                MouseButton = MouseButton.Left,
            });

            var panel = target.Presenter.Panel;

            Assert.Equal(
                KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel),
                panel.Children[1]);
        }
示例#9
0
        public void Setting_SelectedItem_Should_Set_Panel_Keyboard_Navigation()
        {
            var target = new ListBox
            {
                Template = new FuncControlTemplate(CreateListBoxTemplate),
                Items = new[] { "Foo", "Bar", "Baz " },
            };

            target.ApplyTemplate();

            target.Presenter.Panel.Children[1].RaiseEvent(new PointerPressEventArgs
            {
                RoutedEvent = InputElement.PointerPressedEvent,
                MouseButton = MouseButton.Left,
            });

            var panel = target.Presenter.Panel;

            Assert.Equal(
                KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel),
                panel.Children[1]);
        }
示例#10
0
        private void Prepare(ListBox target)
        {
            // Apply the template to the ListBox itself.
            target.ApplyTemplate();

            // Then to its inner ScrollViewer.
            var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();

            scrollViewer.ApplyTemplate();

            // Then make the ScrollViewer create its child.
            ((ContentPresenter)scrollViewer.Presenter).UpdateChild();

            // Now the ItemsPresenter should be reigstered, so apply its template.
            target.Presenter.ApplyTemplate();

            // Because ListBox items are virtualized we need to do a layout to make them appear.
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            // Now set and apply the item templates.
            foreach (ListBoxItem item in target.Presenter.Panel.Children)
            {
                item.Template = ListBoxItemTemplate();
                item.ApplyTemplate();
                item.Presenter.ApplyTemplate();
                ((ContentPresenter)item.Presenter).UpdateChild();
            }

            // The items were created before the template was applied, so now we need to go back
            // and re-arrange everything.
            foreach (IControl i in target.GetSelfAndVisualDescendents())
            {
                i.InvalidateMeasure();
            }

            target.Arrange(new Rect(0, 0, 100, 100));
        }
示例#11
0
        public void DataContexts_Should_Be_Correctly_Set()
        {
            var items = new object[]
            {
                "Foo",
                new Item("Bar"),
                new TextBlock {
                    Text = "Baz"
                },
                new ListBoxItem {
                    Content = "Qux"
                },
            };

            var target = new ListBox
            {
                Template      = new FuncControlTemplate(CreateListBoxTemplate),
                DataContext   = "Base",
                DataTemplates = new DataTemplates
                {
                    new FuncDataTemplate <Item>(x => new Button {
                        Content = x
                    })
                },
                Items = items,
            };

            target.ApplyTemplate();

            var dataContexts = target.Presenter.Panel.Children
                               .Cast <Control>()
                               .Select(x => x.DataContext)
                               .ToList();

            Assert.Equal(
                new object[] { items[0], items[1], "Base", "Base" },
                dataContexts);
        }
示例#12
0
        public void MultiSelect_ModifySelectedItems()
        {
            var lb = new ListBox {
                SelectionMode = SelectionMode.Multiple
            };

            for (int i = 0; i < 5; i++)
            {
                lb.Items.Add(new object());
            }

            CreateAsyncTest(lb,
                            () => lb.ApplyTemplate(),
                            () => {
                for (int i = 0; i < lb.Items.Count; i++)
                {
                    var item = lb.Items [i];
                    lb.SelectedItems.Add(item);
                    var container = (ListBoxItem)lb.ItemContainerGenerator.ContainerFromItem(item);
                    Assert.IsTrue(container.IsSelected, "#1." + i);
                }
            }
                            );
        }
示例#13
0
        public void Replacing_Selected_Item_Should_Update_SelectedItem()
        {
            var items = new ObservableCollection <string>
            {
                "Foo",
                "Bar",
                "Baz"
            };

            var target = new ListBox
            {
                Template      = Template(),
                Items         = items,
                SelectedIndex = 1,
            };

            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            items[1] = "Qux";

            Assert.Equal(-1, target.SelectedIndex);
            Assert.Null(target.SelectedItem);
        }
示例#14
0
        public void Removing_Item_Before_SelectedItem_Should_Update_SelectedIndex()
        {
            var items = new ObservableCollection <string>
            {
                "Foo",
                "Bar",
                "Baz"
            };

            var target = new ListBox
            {
                Template      = Template(),
                Items         = items,
                SelectedIndex = 1,
            };

            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            items.RemoveAt(0);

            Assert.Equal(0, target.SelectedIndex);
            Assert.Equal("Bar", target.SelectedItem);
        }
示例#15
0
        public void Left_Click_On_SelectedItem_Should_Clear_Existing_Selection()
        {
            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,
            };

            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));
        }
示例#16
0
        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", "Qux", "Baz" }, target.SelectedItems);
        }
示例#17
0
        private void Prepare(ListBox target)
        {
            // The ListBox needs to be part of a rooted visual tree.
            var root = new TestRoot();
            root.Child = target;

            // Apply the template to the ListBox itself.
            target.ApplyTemplate();

            // Then to its inner ScrollViewer.
            var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
            scrollViewer.ApplyTemplate();

            // Then make the ScrollViewer create its child.
            ((ContentPresenter)scrollViewer.Presenter).UpdateChild();

            // Now the ItemsPresenter should be reigstered, so apply its template.
            target.Presenter.ApplyTemplate();

            // Because ListBox items are virtualized we need to do a layout to make them appear.
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            // Now set and apply the item templates.
            foreach (ListBoxItem item in target.Presenter.Panel.Children)
            {
                item.Template = ListBoxItemTemplate();
                item.ApplyTemplate();
                item.Presenter.ApplyTemplate();
                ((ContentPresenter)item.Presenter).UpdateChild();
            }

            // The items were created before the template was applied, so now we need to go back
            // and re-arrange everything.
            foreach (IControl i in target.GetSelfAndVisualDescendents())
            {
                i.InvalidateMeasure();
            }

            target.Arrange(new Rect(0, 0, 100, 100));
        }
示例#18
0
        private void ApplyTemplate(ListBox target)
        {
            // Apply the template to the ListBox itself.
            target.ApplyTemplate();

            // Then to its inner ScrollViewer.
            var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
            scrollViewer.ApplyTemplate();

            // Then make the ScrollViewer create its child.
            ((ContentPresenter)scrollViewer.Presenter).UpdateChild();

            // Now the ItemsPresenter should be reigstered, so apply its template.
            target.Presenter.ApplyTemplate();
        }
        public void Nested_ListBox_Does_Not_Change_Parent_SelectedIndex()
        {
            SelectingItemsControl nested;

            var root = new SelectingItemsControl
            {
                Template = Template(),
                Items = new IControl[]
                {
                    new Border(),
                    nested = new ListBox
                    {
                        Template = Template(),
                        Items = new[] { "foo", "bar" },
                        SelectedIndex = 1,
                    }
                },
                SelectedIndex = 0,
            };

            root.ApplyTemplate();
            root.Presenter.ApplyTemplate();
            nested.ApplyTemplate();
            nested.Presenter.ApplyTemplate();

            Assert.Equal(0, root.SelectedIndex);
            Assert.Equal(1, nested.SelectedIndex);

            nested.SelectedIndex = 0;

            Assert.Equal(0, root.SelectedIndex);
        }
        public void Removing_SelectedItem_Should_Clear_TabOnceActiveElement()
        {
            var items = new ObservableCollection<string>(new[] { "Foo", "Bar", "Baz " });

            var target = new ListBox
            {
                Template = Template(),
                Items = items,
            };

            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            target.Presenter.Panel.Children[1].RaiseEvent(new PointerPressedEventArgs
            {
                RoutedEvent = InputElement.PointerPressedEvent,
                MouseButton = MouseButton.Left,
            });

            items.RemoveAt(1);

            var panel = target.Presenter.Panel;

            Assert.Null(KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel));
        }
示例#21
0
        public void Setting_Item_IsSelected_Sets_ListBox_Selection()
        {
            var target = new ListBox
            {
                Template = new ControlTemplate(CreateListBoxTemplate),
                Items = new[] { "Foo", "Bar", "Baz " },
            };

            target.ApplyTemplate();

            ((ListBoxItem)target.GetLogicalChildren().ElementAt(1)).IsSelected = true;

            Assert.Equal("Bar", target.SelectedItem);
            Assert.Equal(1, target.SelectedIndex);
        }
示例#22
0
        public void Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected()
        {
            var target = new ListBox
            {
                Template = new ControlTemplate(CreateListBoxTemplate),
                Items = new[] { "Foo", "Bar", "Baz " },
                SelectionMode = SelectionMode.Toggle | SelectionMode.AlwaysSelected,
            };

            target.ApplyTemplate();
            target.SelectedIndex = 0;

            target.Presenter.Panel.Children[0].RaiseEvent(new PointerPressEventArgs
            {
                RoutedEvent = InputElement.PointerPressedEvent,
                MouseButton = MouseButton.Left,
            });

            Assert.Equal(0, target.SelectedIndex);
        }