Пример #1
0
        private static void UpdateItemsView(ItemsControl sender, AvaloniaPropertyChangedEventArgs e)
        {
            if (e.NewValue is null)
            {
                sender.ItemsPanel = ItemsControl.ItemsPanelProperty.GetDefaultValue(sender.GetType());
                sender.DataTemplates.Clear();

                return;
            }

            var view = (ItemsControlView)e.NewValue;

            sender.ItemsPanel = view.PanelTemplate;

            sender.DataTemplates.Clear();
            sender.DataTemplates.AddRange(view.DataTemplates);

            var template = sender.GetBaseValue(TemplatedControl.TemplateProperty, BindingPriority.LocalValue);

            sender.SetValue(TemplatedControl.TemplateProperty, null);
            sender.ApplyTemplate();

            if (template.HasValue)
            {
                sender.SetValue(TemplatedControl.TemplateProperty, template.Value);
            }
            else
            {
                sender.ClearValue(TemplatedControl.TemplateProperty);
            }

            sender.ApplyTemplate();
        }
Пример #2
0
        public void Presenter_Items_Should_Be_In_Sync()
        {
            var target = new ItemsControl
            {
                Template = GetTemplate(),
                Items    = new object[]
                {
                    new Button(),
                    new Button(),
                },
            };

            var root = new TestRoot {
                Child = target
            };
            var otherPanel = new StackPanel();

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

            target.ItemContainerGenerator.Materialized += (s, e) =>
            {
                Assert.IsType <Canvas>(e.Containers[0].Item);
            };

            target.Items = new[]
            {
                new Canvas()
            };
        }
Пример #3
0
        public void When_EarlyItems()
        {
            var style = new Style(typeof(Windows.UI.Xaml.Controls.ItemsControl))
            {
                Setters =
                {
                    new Setter <ItemsControl>("Template", t =>
                                              t.Template = Funcs.Create(() =>
                                                                        new ItemsPresenter()
                                                                        )
                                              )
                }
            };

            var panel = new StackPanel();

            var SUT = new ItemsControl()
            {
                Style      = style,
                ItemsPanel = new ItemsPanelTemplate(() => panel),
                Items      =
                {
                    new Border {
                        Name = "b1"
                    }
                }
            };

            SUT.ApplyTemplate();

            // Search on the panel for now, as the name lookup is not properly
            // aligned on net46.
            Assert.IsNotNull(panel.FindName("b1"));
        }
Пример #4
0
        public void When_EarlyItems()
        {
            var style = new Style(typeof(Windows.UI.Xaml.Controls.ItemsControl))
            {
                Setters =
                {
                    new Setter <ItemsControl>("Template", t =>
                                              t.Template = Funcs.Create(() =>
                                                                        new ItemsPresenter()
                                                                        )
                                              )
                }
            };

            var panel = new StackPanel();

            var SUT = new ItemsControl()
            {
                Style      = style,
                ItemsPanel = new ItemsPanelTemplate(() => panel),
                Items      =
                {
                    new Border {
                        Name = "b1"
                    }
                }
            };

            new Grid().Children.Add(SUT);             // This is enough for now, but the `SUT` should be in the visual tree for its template to get applied
            SUT.ApplyTemplate();

            // Search on the panel for now, as the name lookup is not properly
            // aligned on net46.
            Assert.IsNotNull(panel.FindName("b1"));
        }
Пример #5
0
        public void LoadedA3()
        {
            bool loaded_before = false;
            bool loaded_after  = false;

            // A user set Template will not walk up the tree and emit its parents loaded events
            ItemsControl c = (ItemsControl)XamlReader.Load(@"
<ItemsControl
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
	<ItemsControl.Template>
		<ControlTemplate>
			<Grid />
		</ControlTemplate>
	</ItemsControl.Template>
</ItemsControl>
");

            // Attach a handler before and after the element is added to the tree
            c.Loaded += (o, e) => { loaded_before = true; };
            TestPanel.Children.Add(c);
            c.Loaded += (o, e) => loaded_after = true;

            // Only one handler is invoked
            EnqueueConditional(() => loaded_before, "#1");
            Enqueue(() => c.ApplyTemplate());
            Enqueue(() => Assert.IsFalse(loaded_after, "#2"));
            EnqueueTestComplete();
        }
Пример #6
0
        public void When_ContainerStyleSet()
        {
            var count = 0;
            var panel = new StackPanel();

            var source = new ObservableVector <int>()
            {
                1, 2, 3
            };

            var SUT = new ItemsControl()
            {
                ItemsPanelRoot         = panel,
                ItemContainerStyle     = BuildBasicContainerStyle(),
                InternalItemsPanelRoot = panel,
                ItemTemplate           = new DataTemplate(() =>
                {
                    count++;
                    return(new Border());
                })
            };

            SUT.ApplyTemplate();

            Assert.AreEqual(0, count);

            SUT.ItemsSource = source;
            Assert.AreEqual(3, count);

            source.Add(4);
            Assert.AreEqual(7, count);

            source.Remove(1);
            Assert.AreEqual(7, count);
        }
Пример #7
0
        public void Focuses_Next_Item_On_Key_Down()
        {
            using (UnitTestApplication.Start(TestServices.RealFocus))
            {
                var items = new object[]
                {
                    new Button(),
                    new Button(),
                };

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

                var root = new TestRoot {
                    Child = target
                };

                target.ApplyTemplate();
                target.Presenter.ApplyTemplate();
                target.Presenter.Panel.Children[0].Focus();

                target.RaiseEvent(new KeyEventArgs
                {
                    RoutedEvent = InputElement.KeyDownEvent,
                    Key         = Key.Down,
                });

                Assert.Equal(
                    target.Presenter.Panel.Children[1],
                    FocusManager.Instance.Current);
            }
        }
Пример #8
0
        /// <summary>
        /// Forces the specified items control to generate containers for its items.
        /// </summary>
        /// <param name="itemsControl">The items control.</param>
        private static void ForceItemsControlToGenerateContainers(ItemsControl itemsControl)
        {
            itemsControl.ApplyTemplate();

            var itemsPresenter = (ItemsPresenter)itemsControl.Template.FindName("ItemsHost", itemsControl);

            if (itemsPresenter != null)
            {
                itemsPresenter.ApplyTemplate();
            }
            else
            {
                // The Tree template has not named the ItemsPresenter, so walk the descendents and
                // find the child.
                itemsPresenter = itemsControl.FindVisualChild <ItemsPresenter>();

                if (itemsPresenter == null)
                {
                    itemsControl.UpdateLayout();

                    itemsPresenter = itemsControl.FindVisualChild <ItemsPresenter>();
                }
            }

            if (itemsPresenter != null)
            {
                var itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);

                // Ensure that the generator for this panel has been created.
                var children = itemsHostPanel.Children;
            }
        }
Пример #9
0
        public void Should_Clear_Containers_When_ItemsPresenter_Changes()
        {
            var target = new ItemsControl
            {
                Items    = new[] { "foo", "bar" },
                Template = GetTemplate(),
            };

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

            Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());

            target.Template = GetTemplate();
            target.ApplyTemplate();

            Assert.Empty(target.ItemContainerGenerator.Containers);
        }
Пример #10
0
        public void Panel_Should_Have_TemplatedParent_Set_To_ItemsControl()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();

            Assert.Equal(target, target.Presenter.Panel.TemplatedParent);
        }
Пример #11
0
        public void Panel_Should_Have_TemplatedParent_Set_To_ItemsControl()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();

            Assert.Equal(target, target.Presenter.Panel.TemplatedParent);
        }
Пример #12
0
        /// <summary>
        /// ay 2015-05-23 15:34:08 增加,不需要查找子容器
        /// </summary>
        /// <param name="container"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static TreeViewItem GetTreeViewItem2(ItemsControl container, object item)
        {
            if (container != null)
            {
                if (container.DataContext == item)
                {
                    return(container as TreeViewItem);
                }
                // Expand the current container
                if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded)
                {
                    container.SetValue(TreeViewItem.IsExpandedProperty, true);
                }
                // Try to generate the ItemsPresenter and the ItemsPanel.
                // by calling ApplyTemplate. Note that in the
                // virtualizing case even if the item is marked
                // expanded we still need to do this step in order to
                // regenerate the visuals because they may have been virtualized away.
                container.ApplyTemplate();
                ItemsPresenter itemsPresenter =
                    (ItemsPresenter)container.Template.FindName("ItemsHost", container);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    // The Tree template has not named the ItemsPresenter,
                    // so walk the descendents and find the child.
                    itemsPresenter = WpfTreeHelper.FindVisualChild <ItemsPresenter>(container);
                    if (itemsPresenter == null)
                    {
                        container.UpdateLayout();
                        itemsPresenter = WpfTreeHelper.FindVisualChild <ItemsPresenter>(container);
                    }
                }

                Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);
                // Ensure that the generator for this panel has been created.
                UIElementCollection      children          = itemsHostPanel.Children;
                MyVirtualizingStackPanel virtualizingPanel =
                    itemsHostPanel as MyVirtualizingStackPanel;
                for (int i = 0, count = container.Items.Count; i < count; i++)
                {
                    TreeViewItem subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);
                    if (subContainer.DataContext == item)
                    {
                        subContainer.IsExpanded = false;
                        return(subContainer as TreeViewItem);
                    }
                }
            }
            return(null);
        }
Пример #13
0
        public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren()
        {
            var target = new ItemsControl();
            var child  = new Control();

            target.Template = GetTemplate();
            target.Items    = new[] { child };
            target.ApplyTemplate();

            Assert.Equal(new[] { child }, ((ILogical)target).LogicalChildren.ToList());
        }
Пример #14
0
        public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren()
        {
            var target = new ItemsControl();
            var child = new Control();

            target.Template = GetTemplate();
            target.Items = new[] { child };
            target.ApplyTemplate();

            Assert.Equal(new[] { child }, ((ILogical)target).LogicalChildren.ToList());
        }
Пример #15
0
        public void Control_Item_Should_Have_Parent_Set()
        {
            var target = new ItemsControl();
            var child = new Control();

            target.Template = GetTemplate();
            target.Items = new[] { child };
            target.ApplyTemplate();

            Assert.Equal(target, child.Parent);
            Assert.Equal(target, ((ILogical)child).LogicalParent);
        }
Пример #16
0
        public void Setting_Items_To_Null_Should_Remove_LogicalChildren()
        {
            var target = new ItemsControl();
            var child  = new Control();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();
            target.Items = null;

            Assert.Equal(new ILogical[0], ((ILogical)target).LogicalChildren.ToList());
        }
Пример #17
0
        public void Control_Item_Should_Have_Parent_Set()
        {
            var target = new ItemsControl();
            var child  = new Control();

            target.Template = GetTemplate();
            target.Items    = new[] { child };
            target.ApplyTemplate();

            Assert.Equal(target, child.Parent);
            Assert.Equal(target, ((ILogical)child).LogicalParent);
        }
Пример #18
0
        public void Item_Should_Have_TemplatedParent_Set_To_Null()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();

            var item = (TextBlock)target.Presenter.Panel.GetVisualChildren().First();

            Assert.Null(item.TemplatedParent);
        }
Пример #19
0
        public void Item_Should_Have_TemplatedParent_Set_To_Null()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();

            var item = (TextBlock)target.Presenter.Panel.GetVisualChildren().First();

            Assert.Null(item.TemplatedParent);
        }
Пример #20
0
        public virtual void ItemTemplateTest3()
        {
            ItemsControl box = (ItemsControl)CurrentControl;

            CurrentControl.ItemTemplate = null;

            TestPanel.Children.Add(box);
            Enqueue(() => CurrentControl.DisplayMemberPath = "Test");
            Enqueue(() => box.ApplyTemplate());
            Enqueue(() => CurrentControl.Items.Add(new object()));
            // Subclasses do the validation
        }
Пример #21
0
        public void Clearing_Control_Item_Should_Clear_Child_Controls_Parent()
        {
            var target = new ItemsControl();
            var child = new Control();

            target.Template = GetTemplate();
            target.Items = new[] { child };
            target.ApplyTemplate();
            target.Items = null;

            Assert.Null(child.Parent);
            Assert.Null(((ILogical)child).LogicalParent);
        }
Пример #22
0
        public void Panel_Should_Have_TemplatedParent_Set_To_ItemsControl()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();

            var presenter = target.GetTemplateChildren().OfType<ItemsPresenter>().Single();
            var panel = target.GetTemplateChildren().OfType<StackPanel>().Single();

            Assert.Equal(target, panel.TemplatedParent);
        }
Пример #23
0
        public virtual void DisableControlTest()
        {
            ItemsControl c = (ItemsControl)CurrentControl;

            CurrentControl.IsEnabled = false;
            CurrentControl.Items.Add(new ListBox());
            CurrentControl.Items.Add(new ComboBox());
            CurrentControl.Items.Add(new Button());
            EnqueueWaitLoaded(c, "#1");
            Enqueue(() => c.ApplyTemplate());

            TestPanel.Children.Add((Control)CurrentControl);
        }
Пример #24
0
        public void Container_Should_Have_TemplatedParent_Set_To_Null()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            var container = (ContentPresenter)target.Presenter.Panel.Children[0];

            Assert.Null(container.TemplatedParent);
        }
Пример #25
0
        public void Panel_Should_Have_TemplatedParent_Set_To_ItemsControl()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();

            var presenter = target.GetTemplateChildren().OfType <ItemsPresenter>().Single();
            var panel     = target.GetTemplateChildren().OfType <StackPanel>().Single();

            Assert.Equal(target, panel.TemplatedParent);
        }
Пример #26
0
        public void Container_Should_Have_TemplatedParent_Set_To_Null()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            var container = (ContentPresenter)target.Presenter.Panel.Children[0];

            Assert.Null(container.TemplatedParent);
        }
Пример #27
0
        public void Clearing_Items_Should_Clear_Child_Controls_Parent()
        {
            var target = new ItemsControl();
            var child  = new Control();

            target.Template = GetTemplate();
            target.Items    = new[] { child };
            target.ApplyTemplate();
            target.Items = null;

            Assert.Null(child.Parent);
            Assert.Null(((ILogical)child).LogicalParent);
        }
Пример #28
0
        public virtual void DisplayMemberPathTest2()
        {
            // Check if 'DisplayMemberPath' is used when a UIElement
            // is added to the ItemsCollection
            ItemsControl c = (ItemsControl)CurrentControl;

            CurrentControl.DisplayMemberPath = "Width";

            TestPanel.Children.Add(c);
            Enqueue(() => c.ItemTemplate = null);
            Enqueue(() => c.ApplyTemplate());
            Enqueue(() => c.Items.Add(new object()));
            // Validation is performed in the subclasses
        }
Пример #29
0
        public void Adding_String_Item_Should_Make_TextBlock_Appear_In_LogicalChildren()
        {
            var target = new ItemsControl();
            var child  = new Control();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();

            var logical = (ILogical)target;

            Assert.Equal(1, logical.LogicalChildren.Count);
            Assert.IsType <TextBlock>(logical.LogicalChildren[0]);
        }
Пример #30
0
        public void Presenter_Items_Should_Be_In_Sync_When_Replacing_ItemTemplate()
        {
            var target = new ItemsControl
            {
                Template = GetTemplate(),
                Items    = new[]
                {
                    new Item("Item1")
                },
                ItemTemplate = new FuncDataTemplate <Item>((x, ns) => new TextBlock())
            };

            var root = new TestRoot {
                Child = target
            };
            var otherPanel = new StackPanel();

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

            int dematerializedEventCallCount = 0;

            target.ItemContainerGenerator.Dematerialized += (s, e) =>
            {
                Assert.IsType <Item>(e.Containers[0].Item);
                Assert.Equal("Item1", ((Item)e.Containers[0].Item).Value);
                var contentPresenter = ((ContentPresenter)e.Containers[0].ContainerControl);
                contentPresenter.UpdateChild();
                Assert.IsType <TextBlock>(contentPresenter.Child);
                dematerializedEventCallCount++;
            };

            int materializedEventCallCount = 0;

            target.ItemContainerGenerator.Materialized += (s, e) =>
            {
                Assert.IsType <Item>(e.Containers[0].Item);
                Assert.Equal("Item1", ((Item)e.Containers[0].Item).Value);
                var contentPresenter = ((ContentPresenter)e.Containers[0].ContainerControl);
                contentPresenter.UpdateChild();
                Assert.IsType <Canvas>(contentPresenter.Child);
                materializedEventCallCount++;
            };

            target.ItemTemplate =
                new FuncDataTemplate <Item>((x, ns) => new Canvas());

            Assert.Equal(1, dematerializedEventCallCount);
            Assert.Equal(1, materializedEventCallCount);
        }
Пример #31
0
        public void Item_Should_Have_TemplatedParent_Set_To_Null()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();

            var presenter = target.GetTemplateChildren().OfType <ItemsPresenter>().Single();
            var panel     = target.GetTemplateChildren().OfType <StackPanel>().Single();
            var item      = (TextBlock)panel.GetVisualChildren().First();

            Assert.Null(item.TemplatedParent);
        }
Пример #32
0
        public void Item_Should_Have_TemplatedParent_Set_To_Null()
        {
            var target = new ItemsControl();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();

            var presenter = target.GetTemplateChildren().OfType<ItemsPresenter>().Single();
            var panel = target.GetTemplateChildren().OfType<StackPanel>().Single();
            var item = (TextBlock)panel.GetVisualChildren().First();

            Assert.Null(item.TemplatedParent);
        }
Пример #33
0
        public void Adding_String_Item_Should_Make_TextBlock_Appear_In_LogicalChildren()
        {
            var target = new ItemsControl();
            var child = new Control();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            var logical = (ILogical)target;
            Assert.Equal(1, logical.LogicalChildren.Count);
            Assert.IsType<TextBlock>(logical.LogicalChildren[0]);
        }
Пример #34
0
        public void Should_Use_ItemTemplate_To_Create_Control()
        {
            var target = new ItemsControl
            {
                Template     = GetTemplate(),
                ItemTemplate = new FuncDataTemplate <string>(_ => new Canvas()),
            };

            target.Items = new[] { "Foo" };
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            Assert.IsType <Canvas>(target.Presenter.Panel.Children[0]);
        }
Пример #35
0
        public virtual void StructureChanged_Events()
        {
            if (!EventsManager.Instance.AutomationSingletonExists)
            {
                EnqueueTestComplete();
                return;
            }

            bool         concreteLoaded = false;
            ItemsControl concrete       = CreateConcreteFrameworkElement() as ItemsControl;

            concrete.Loaded += (o, e) => concreteLoaded = true;
            TestPanel.Children.Add(concrete);

            EnqueueConditional(() => concreteLoaded, "ConcreteLoaded #0");
            Enqueue(() => concrete.ApplyTemplate());
            Enqueue(() => {
                AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(concrete);
                Assert.IsNotNull(peer, "FrameworkElementAutomationPeer.CreatePeerForElement #0");
                AutomationEventTuple tuple
                    = EventsManager.Instance.GetAutomationEventFrom(peer, AutomationEvents.StructureChanged);
                Assert.IsNull(tuple, "GetAutomationEventFrom #0");
            });
            Enqueue(() => {
                EventsManager.Instance.Reset();
                concrete.Items.Add(new ListBoxItem()
                {
                    Content = "Item 0"
                });
            });
            Enqueue(() => {
                AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(concrete);
                Assert.IsNotNull(peer, "FrameworkElementAutomationPeer.CreatePeerForElement #1");
                AutomationEventTuple tuple
                    = EventsManager.Instance.GetAutomationEventFrom(peer, AutomationEvents.StructureChanged);
                Assert.IsNotNull(tuple, "GetAutomationEventFrom #1");
            });
            Enqueue(() => {
                EventsManager.Instance.Reset();
                concrete.Items.RemoveAt(0);
            });
            Enqueue(() => {
                AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(concrete);
                Assert.IsNotNull(peer, "FrameworkElementAutomationPeer.CreatePeerForElement #2");
                AutomationEventTuple tuple
                    = EventsManager.Instance.GetAutomationEventFrom(peer, AutomationEvents.StructureChanged);
                Assert.IsNotNull(tuple, "GetAutomationEventFrom #2");
            });
            EnqueueTestComplete();
        }
Пример #36
0
        public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren()
        {
            var target = new ItemsControl();
            var child = new Control();

            target.Template = GetTemplate();
            target.Items = new[] { child };

            // Should appear both before and after applying template.
            Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());

            target.ApplyTemplate();

            Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());
        }
Пример #37
0
        public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren()
        {
            var target = new ItemsControl();
            var child  = new Control();

            target.Template = GetTemplate();
            target.Items    = new[] { child };

            // Should appear both before and after applying template.
            Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());

            target.ApplyTemplate();

            Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());
        }
Пример #38
0
        public void Changing_Items_Should_Fire_LogicalChildren_CollectionChanged()
        {
            var target = new ItemsControl();
            var child  = new Control();
            var called = false;

            target.Template = GetTemplate();
            target.Items    = new[] { child };
            target.ApplyTemplate();

            ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;

            target.Items = new[] { "Foo" };

            Assert.True(called);
        }
Пример #39
0
        public void Setting_Items_To_Null_Should_Remove_LogicalChildren()
        {
            var target = new ItemsControl();
            var child  = new Control();

            target.Template = GetTemplate();
            target.Items    = new[] { "Foo" };
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            Assert.NotEmpty(target.GetLogicalChildren());

            target.Items = null;

            Assert.Equal(new ILogical[0], target.GetLogicalChildren());
        }
Пример #40
0
        public void Setting_Items_To_Null_Should_Fire_LogicalChildren_CollectionChanged()
        {
            var target = new ItemsControl();
            var child  = new Control();
            var called = false;

            target.Template = GetTemplate();
            target.Items    = new[] { child };
            target.ApplyTemplate();

            ((ILogical)target).LogicalChildren.CollectionChanged                   += (s, e) =>
                                                                             called = e.Action == NotifyCollectionChangedAction.Remove;

            target.Items = null;

            Assert.True(called);
        }
Пример #41
0
        public void Should_Use_ItemTemplate_To_Create_Control()
        {
            var target = new ItemsControl
            {
                Template = GetTemplate(),
                ItemTemplate = new FuncDataTemplate<string>(_ => new Canvas()),
            };

            target.Items = new[] { "Foo" };
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            var container = (ContentPresenter)target.Presenter.Panel.Children[0];
            container.UpdateChild();

            Assert.IsType<Canvas>(container.Child);
        }
Пример #42
0
        public void Nested_TemplatedControls_Should_Be_Expanded_And_Have_Correct_TemplatedParent()
        {
            var target = new ItemsControl
            {
                Template = new ControlTemplate<ItemsControl>(ItemsControlTemplate),
                Items = new[] { "Foo", }
            };

            target.ApplyTemplate();

            var scrollViewer = target.GetVisualDescendents()
                .OfType<ScrollViewer>()
                .Single();
            var types = target.GetVisualDescendents()
                .Select(x => x.GetType())
                .ToList();
            var templatedParents = target.GetVisualDescendents()
                .OfType<IControl>()
                .Select(x => x.TemplatedParent)
                .ToList();

            Assert.Equal(
                new[]
                {
                    typeof(Border),
                    typeof(ScrollViewer),
                    typeof(ScrollContentPresenter),
                    typeof(ItemsPresenter),
                    typeof(StackPanel),
                    typeof(TextBlock),
                },
                types);

            Assert.Equal(
                new object[]
                {
                    target,
                    target,
                    scrollViewer,
                    target,
                    target,
                    null
                },
                templatedParents);
        }
Пример #43
0
        public void Adding_Items_Should_Fire_LogicalChildren_CollectionChanged()
        {
            var target = new ItemsControl();
            var items = new PerspexList<string> { "Foo" };
            var called = false;

            target.Template = GetTemplate();
            target.Items = items;
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
                called = e.Action == NotifyCollectionChangedAction.Add;

            items.Add("Bar");

            Assert.True(called);
        }
Пример #44
0
        public void Container_Child_Should_Have_LogicalParent_Set_To_Container()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var root = new Window();
                var target = new ItemsControl();

                root.Content = target;

                var templatedParent = new Button();
                target.TemplatedParent = templatedParent;
                target.Template = GetTemplate();

                target.Items = new[] { "Foo" };

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

                var container = (ContentPresenter)target.Presenter.Panel.Children[0];

                Assert.Equal(container, container.Child.Parent);
            }
        }
Пример #45
0
        public void Setting_Items_To_Null_Should_Fire_LogicalChildren_CollectionChanged()
        {
            var target = new ItemsControl();
            var child = new Control();
            var called = false;

            target.Template = GetTemplate();
            target.Items = new[] { child };
            target.ApplyTemplate();

            ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
                called = e.Action == NotifyCollectionChangedAction.Remove;

            target.Items = null;

            Assert.True(called);
        }
Пример #46
0
        public void Changing_Items_Should_Fire_LogicalChildren_CollectionChanged()
        {
            var target = new ItemsControl();
            var child = new Control();
            var called = false;

            target.Template = GetTemplate();
            target.Items = new[] { child };
            target.ApplyTemplate();

            ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;

            target.Items = new[] { "Foo" };

            Assert.True(called);
        }
Пример #47
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 ItemsControl
            {
                Template = GetTemplate(),
                DataContext = "Base",
                DataTemplates = new DataTemplates
                {
                    new FuncDataTemplate<Item>(x => new Button { Content = x })
                },
                Items = items,
            };

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

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

            Assert.Equal(
                new object[] { items[0], items[1], "Base", "Base" },
                dataContexts);
        }
Пример #48
0
        public void MemberSelector_Should_Select_Member()
        {
            var target = new ItemsControl
            {
                Template = GetTemplate(),
                Items = new[] { new Item("Foo"), new Item("Bar") },
                MemberSelector = new FuncMemberSelector<Item, string>(x => x.Value),
            };

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

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

            Assert.Equal(new[] { "Foo", "Bar" }, text);
        }
Пример #49
0
        public void Setting_Items_To_Null_Should_Remove_LogicalChildren()
        {
            var target = new ItemsControl();
            var child = new Control();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();
            target.Items = null;

            Assert.Equal(new ILogical[0], ((ILogical)target).LogicalChildren.ToList());
        }
Пример #50
0
        public void DataTemplate_Created_Content_Should_Be_NameScope()
        {
            var items = new object[]
            {
                "foo",
            };

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

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

            var container = (ContentPresenter)target.Presenter.Panel.LogicalChildren[0];
            container.UpdateChild();

            Assert.NotNull(NameScope.GetNameScope((TextBlock)container.Child));
        }
Пример #51
0
        public void Control_Item_Should_Not_Be_NameScope()
        {
            var items = new object[]
            {
                new TextBlock(),
            };

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

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

            var item = target.Presenter.Panel.LogicalChildren[0];
            Assert.Null(NameScope.GetNameScope((TextBlock)item));
        }
Пример #52
0
        public void Setting_Items_To_Null_Should_Remove_LogicalChildren()
        {
            var target = new ItemsControl();
            var child = new Control();

            target.Template = GetTemplate();
            target.Items = new[] { "Foo" };
            target.ApplyTemplate();
            target.Presenter.ApplyTemplate();

            Assert.NotEmpty(target.GetLogicalChildren());

            target.Items = null;

            Assert.Equal(new ILogical[0], target.GetLogicalChildren());
        }