Пример #1
0
        public void Clearing_Items_Should_Remove_Containers()
        {
            var items = new ObservableCollection<string> { "foo", "bar" };
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.Clear();

            Assert.Empty(target.Panel.Children);
            Assert.Empty(target.ItemContainerGenerator.Containers);
        }
Пример #2
0
        public void Should_Add_Containers()
        {
            var target = new ItemsPresenter
            {
                Items = new[] { "foo", "bar" },
            };

            target.ApplyTemplate();

            Assert.Equal(2, target.Panel.Children.Count);
            Assert.IsType<ContentPresenter>(target.Panel.Children[0]);
            Assert.IsType<ContentPresenter>(target.Panel.Children[1]);
            Assert.Equal("foo", ((ContentPresenter)target.Panel.Children[0]).Content);
            Assert.Equal("bar", ((ContentPresenter)target.Panel.Children[1]).Content);
        }
Пример #3
0
        public void Should_Add_Containers_Of_Correct_Type()
        {
            var target = new ItemsPresenter
            {
                Items = new[] { "foo", "bar" },
            };

            target.ItemContainerGenerator = new ItemContainerGenerator<ListBoxItem>(
                target, 
                ListBoxItem.ContentProperty,
                null);
            target.ApplyTemplate();

            Assert.Equal(2, target.Panel.Children.Count);
            Assert.IsType<ListBoxItem>(target.Panel.Children[0]);
            Assert.IsType<ListBoxItem>(target.Panel.Children[1]);
        }
Пример #4
0
        public void MemberSelector_Should_Select_Member()
        {
            var target = new ItemsPresenter
            {
                Items = new[] { new Item("Foo"), new Item("Bar") },
                MemberSelector = new FuncMemberSelector<Item, string>(x => x.Value),
            };

            target.ApplyTemplate();

            var text = target.Panel.Children
                .Cast<ContentPresenter>()
                .Select(x => x.Content)
                .ToList();

            Assert.Equal(new[] { "Foo", "Bar" }, text);
        }
Пример #5
0
        public void Inserting_Items_Should_Update_Containers()
        {
            var items = new ObservableCollection<string> { "foo", "bar", "baz" };
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.Insert(2, "insert");

            var text = target.Panel.Children
                .OfType<ContentPresenter>()
                .Select(x => x.Content)
                .ToList();

            Assert.Equal(new[] { "foo", "bar", "insert", "baz" }, text);
        }
Пример #6
0
        public void Should_Remove_Containers()
        {
            var items = new AvaloniaList<string>(new[] { "foo", "bar" });
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.RemoveAt(0);

            Assert.Equal(1, target.Panel.Children.Count);
            Assert.Equal("bar", ((ContentPresenter)target.Panel.Children[0]).Content);
            Assert.Equal("bar", ((ContentPresenter)target.ItemContainerGenerator.ContainerFromIndex(0)).Content);
        }
Пример #7
0
        public void MemberSelector_Should_Set_DataContext()
        {
            var items = new[] { new Item("Foo"), new Item("Bar") };
            var target = new ItemsPresenter
            {
                Items = items,
                MemberSelector = new FuncMemberSelector<Item, string>(x => x.Value),
            };

            target.ApplyTemplate();

            var dataContexts = target.Panel.Children
                .Cast<ContentPresenter>()
                .Do(x => x.UpdateChild())
                .Select(x => x.DataContext)
                .ToList();

            Assert.Equal(new[] { "Foo", "Bar" }, dataContexts);
        }
Пример #8
0
        public void Panel_Should_Be_Visual_Child()
        {
            var target = new ItemsPresenter();

            target.ApplyTemplate();

            var child = target.GetVisualChildren().Single();

            Assert.Equal(target.Panel, child);
        }
Пример #9
0
        public void Panel_TabNavigation_Should_Be_Set_To_ItemsPresenter_Value()
        {
            var target = new ItemsPresenter();

            KeyboardNavigation.SetTabNavigation(target, KeyboardNavigationMode.Cycle);
            target.ApplyTemplate();

            Assert.Equal(KeyboardNavigationMode.Cycle, KeyboardNavigation.GetTabNavigation((InputElement)target.Panel));
        }
Пример #10
0
        public void Panel_Should_Be_Created_From_ItemsPanel_Template()
        {
            var panel = new Panel();
            var target = new ItemsPresenter
            {
                ItemsPanel = new FuncTemplate<IPanel>(() => panel),
            };

            target.ApplyTemplate();

            Assert.Same(panel, target.Panel);
            Assert.Same(target, target.Panel.Parent);
        }
Пример #11
0
        public void Should_Handle_Duplicate_Items()
        {
            var items = new AvaloniaList<int>(new[] { 1, 2, 1 });

            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.RemoveAt(2);

            var numbers = target.Panel.Children
                .OfType<ContentPresenter>()
                .Select(x => x.Content)
                .Cast<int>();
            Assert.Equal(new[] { 1, 2 }, numbers);
        }
Пример #12
0
        public void Inserting_Then_Removing_Should_Add_Remove_Containers()
        {
            var items = new AvaloniaList<string>(Enumerable.Range(0, 5).Select(x => $"Item {x}"));
            var toAdd = Enumerable.Range(0, 3).Select(x => $"Added Item {x}").ToArray();
            var target = new ItemsPresenter
            {
                VirtualizationMode = ItemVirtualizationMode.None,
                Items = items,
                ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 }),
            };

            target.ApplyTemplate();

            Assert.Equal(items.Count, target.Panel.Children.Count);

            foreach (var item in toAdd)
            {
                items.Insert(1, item);
            }

            Assert.Equal(items.Count, target.Panel.Children.Count);

            foreach (var item in toAdd)
            {
                items.Remove(item);
            }

            Assert.Equal(items.Count, target.Panel.Children.Count);
        }
Пример #13
0
        public void Should_Handle_Null_Items()
        {
            var items = new AvaloniaList<string>(new[] { "foo", null, "bar" });

            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();

            var text = target.Panel.Children.Cast<ContentPresenter>().Select(x => x.Content).ToList();

            Assert.Equal(new[] { "foo", null, "bar" }, text);
            Assert.NotNull(target.ItemContainerGenerator.ContainerFromIndex(0));
            Assert.NotNull(target.ItemContainerGenerator.ContainerFromIndex(1));
            Assert.NotNull(target.ItemContainerGenerator.ContainerFromIndex(2));

            items.RemoveAt(1);

            text = target.Panel.Children.Cast<ContentPresenter>().Select(x => x.Content).ToList();

            Assert.Equal(new[] { "foo", "bar" }, text);
            Assert.NotNull(target.ItemContainerGenerator.ContainerFromIndex(0));
            Assert.NotNull(target.ItemContainerGenerator.ContainerFromIndex(1));
        }
Пример #14
0
        public void Setting_Items_To_Null_Should_Remove_Containers()
        {
            var target = new ItemsPresenter
            {
                Items = new[] { "foo", "bar" },
            };

            target.ApplyTemplate();
            target.Items = null;

            Assert.Empty(target.Panel.Children);
            Assert.Empty(target.ItemContainerGenerator.Containers);
        }
Пример #15
0
        public void Setting_Presenter_Explicitly_Should_Set_Item_Parent()
        {
            var target = new TestItemsControl();
            var child = new Control();

            var presenter = new ItemsPresenter
            {
                TemplatedParent = target,
                [~ItemsPresenter.ItemsProperty] = target[~ItemsControl.ItemsProperty],
            };

            presenter.ApplyTemplate();
            target.Presenter = presenter;
            target.Items = new[] { child };
            target.ApplyTemplate();

            Assert.Equal(target, child.Parent);
            Assert.Equal(target, ((ILogical)child).LogicalParent);
        }
        public void Should_Return_IsLogicalScrollEnabled_False_When_Doesnt_Have_ScrollPresenter_Parent()
        {
            var target = new ItemsPresenter
            {
                ItemsPanel = VirtualizingPanelTemplate(),
                ItemTemplate = ItemTemplate(),
                VirtualizationMode = ItemVirtualizationMode.Simple,
            };

            target.ApplyTemplate();

            Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
        }