public void Clearing_Items_Should_Clear_Selection()
        {
            var items = new PerspexList<Item>
            {
                new Item(),
                new Item(),
            };

            var target = new Target
            {
                Items = items,
                Template = this.Template(),
            };

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

            Assert.Equal(items[1], target.SelectedItem);
            Assert.Equal(1, target.SelectedIndex);

            target.Items = null;

            Assert.Equal(null, target.SelectedItem);
            Assert.Equal(-1, target.SelectedIndex);
        }
示例#2
0
        public void Items_Passed_To_Constructor_Should_Appear_In_List()
        {
            var items = new[] { 1, 2, 3 };
            var target = new PerspexList<int>(items);

            Assert.Equal(items, target);
        }
示例#3
0
        public void Should_Remove_Containers()
        {
            var items = new PerspexList<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", ((TextBlock)target.Panel.Children[0]).Text);
        }
示例#4
0
        public void Control_Item_Should_Be_Removed_From_Logical_Children_Before_ApplyTemplate()
        {
            var target = new ItemsControl();
            var child = new Control();
            var items = new PerspexList<Control>(child);

            target.Template = GetTemplate();
            target.Items = items;
            items.RemoveAt(0);

            Assert.Null(child.Parent);
            Assert.Null(child.GetLogicalParent());
            Assert.Empty(target.GetLogicalChildren());
        }
        public void First_Item_Should_Be_Selected_When_Added()
        {
            var items = new PerspexList<string>();
            var target = new TestSelector
            {
                Items = items,
                Template = Template(),
            };

            target.ApplyTemplate();
            items.Add("foo");

            Assert.Equal(0, target.SelectedIndex);
            Assert.Equal("foo", target.SelectedItem);
        }
        public void First_Item_Should_Be_Selected_When_Added()
        {
            var items = new PerspexList<string>();
            var target = new SelectingItemsControl
            {
                AutoSelect = true,
                Items = items,
                Template = this.Template(),
            };

            target.ApplyTemplate();
            items.Add("foo");

            Assert.Equal(0, target.SelectedIndex);
            Assert.Equal("foo", target.SelectedItem);
        }
        public void Item_Should_Be_Selected_When_Selection_Removed()
        {
            var items = new PerspexList<string>(new[] { "foo", "bar", "baz", "qux" });

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

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

            Assert.Equal(2, target.SelectedIndex);
            Assert.Equal("qux", target.SelectedItem);
        }
示例#8
0
        public void Adding_Items_Should_Fire_LogicalChildren_CollectionChanged()
        {
            var target = new ItemsControl();
            var items = new PerspexList<string> { "Foo" };
            var called = false;

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

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

            items.Add("Bar");

            Assert.True(called);
        }
        public void Selection_Should_Be_Cleared_When_No_Items_Left()
        {
            var items = new PerspexList<string>(new[] { "foo", "bar" });

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

            target.ApplyTemplate();
            target.SelectedIndex = 1;
            items.RemoveAt(1);
            items.RemoveAt(0);

            Assert.Equal(-1, target.SelectedIndex);
            Assert.Null(target.SelectedItem);
        }
示例#10
0
        public void Adding_Items_Should_Raise_CollectionChanged()
        {
            var target = new PerspexList<int>(new[] { 1, 2 });
            var raised = false;

            target.CollectionChanged += (s, e) =>
            {
                Assert.Equal(target, s);
                Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
                Assert.Equal(new[] { 3, 4 }, e.NewItems.Cast<int>());
                Assert.Equal(2, e.NewStartingIndex);

                raised = true;
            };

            target.AddRange(new[] { 3, 4 });

            Assert.True(raised);
        }
示例#11
0
        public void Removing_Items_Should_Fire_LogicalChildren_CollectionChanged()
        {
            var target = new ItemsControl();
            var items  = new PerspexList <string> {
                "Foo", "Bar"
            };
            var called = false;

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

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

            items.Remove("Bar");

            Assert.True(called);
        }
        public void Adding_Selected_Item_Should_Update_Selection()
        {
            var items = new PerspexList<Item>(new[]
            {
                new Item(),
                new Item(),
            });

            var target = new SelectingItemsControl
            {
                Items = items,
                Template = this.Template(),
            };

            target.ApplyTemplate();
            items.Add(new Item { IsSelected = true });

            Assert.Equal(2, target.SelectedIndex);
            Assert.Equal(items[2], target.SelectedItem);
        }
示例#13
0
        public object ConvertFrom(IValueContext context, CultureInfo culture, object value)
        {
            var result = new PerspexList <T>();
            var values = ((string)value).Split(',');

            foreach (var s in values)
            {
                object v;

                if (TypeUtilities.TryConvert(typeof(T), s, culture, out v))
                {
                    result.Add((T)v);
                }
                else
                {
                    throw new InvalidCastException($"Could not convert '{s}' to {typeof(T)}.");
                }
            }

            return(result);
        }
        public void Adding_Selected_Item_Should_Update_Selection()
        {
            var items = new PerspexList <Item>(new[]
            {
                new Item(),
                new Item(),
            });

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

            target.ApplyTemplate();
            items.Add(new Item {
                IsSelected = true
            });

            Assert.Equal(2, target.SelectedIndex);
            Assert.Equal(items[2], target.SelectedItem);
        }
示例#15
0
 public Node()
 {
     Children = new PerspexList<Node>();
 }
示例#16
0
        public void Clearing_Items_Should_Raise_CollectionChanged_Remove()
        {
            var target = new PerspexList<int>(new[] { 1, 2, 3 });
            var raised = false;

            target.CollectionChanged += (s, e) =>
            {
                Assert.Equal(target, s);
                Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
                Assert.Equal(new[] { 1, 2, 3 }, e.OldItems.Cast<int>());
                Assert.Equal(0, e.OldStartingIndex);

                raised = true;
            };

            target.Clear();

            Assert.True(raised);
        }
示例#17
0
        public void RemoveAll_With_Null_Should_Throw_Exception()
        {
            var target = new PerspexList<int>();

            Assert.Throws<ArgumentNullException>(() => target.RemoveAll(null));
        }
示例#18
0
        public void Should_Handle_Null_Items()
        {
            var items = new PerspexList<string>(new[] { "foo", null, "bar" });

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

            target.ApplyTemplate();

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

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

            items.RemoveAt(1);

            text = target.Panel.Children.Cast<TextBlock>().Select(x => x.Text).ToList();

            Assert.Equal(new[] { "foo", "bar" }, text);
            Assert.NotNull(target.ItemContainerGenerator.ContainerFromIndex(0));
            Assert.NotNull(target.ItemContainerGenerator.ContainerFromIndex(1));
        }
示例#19
0
        public void Should_Handle_Duplicate_Items()
        {
            var items = new PerspexList<int>(new[] { 1, 2, 1 });

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

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

            var text = target.Panel.Children.OfType<TextBlock>().Select(x => x.Text);
            Assert.Equal(new[] { "1", "2" }, text);
        }
示例#20
0
文件: Visual.cs 项目: Scellow/Perspex
        /// <summary>
        /// Initializes a new instance of the <see cref="Visual"/> class.
        /// </summary>
        public Visual()
        {
            this.visualLogger = Log.ForContext(new[]
            {
                new PropertyEnricher("Area", "Visual"),
                new PropertyEnricher("SourceContext", this.GetType()),
                new PropertyEnricher("Id", this.GetHashCode()),
            });

            this.visualChildren = new PerspexList<IVisual>();
            this.visualChildren.CollectionChanged += this.VisualChildrenChanged;
        }
示例#21
0
        public void InsertRange_With_Null_Should_Throw_Exception()
        {
            var target = new PerspexList<int>();

            Assert.Throws<ArgumentNullException>(() => target.InsertRange(1, null));
        }
示例#22
0
        public void InsertRange_Past_End_Should_Throw_Exception()
        {
            var target = new PerspexList<int>();

            Assert.Throws<ArgumentOutOfRangeException>(() => target.InsertRange(1, new List<int>() { 1 }));
        }
示例#23
0
        public void Should_Handle_Null_Items()
        {
            var items = new PerspexList<string>(new[] { "foo", null, "bar" });

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

            target.ApplyTemplate();

            var text = target.Panel.Children.OfType<TextBlock>().Select(x => x.Text).ToList();
            Assert.Equal(new[] { "foo", "bar" }, text);

            items.RemoveAt(1);

            text = target.Panel.Children.OfType<TextBlock>().Select(x => x.Text).ToList();
            Assert.Equal(new[] { "foo", "bar" }, text);
        }
示例#24
0
        public void InsertRange_With_Null_Should_Throw_Exception()
        {
            var target = new PerspexList <int>();

            Assert.Throws <ArgumentNullException>(() => target.InsertRange(1, null));
        }
示例#25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ItemsControl"/> class.
 /// </summary>
 public ItemsControl()
 {
     Classes.Add(":empty");
     Items = new PerspexList <object>();
 }
        public void Setting_Items_To_Null_Should_Clear_Selection()
        {
            var items = new PerspexList<Item>
            {
                new Item(),
                new Item(),
            };

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

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

            Assert.Equal(items[1], target.SelectedItem);
            Assert.Equal(1, target.SelectedIndex);

            target.Items = null;

            Assert.Equal(null, target.SelectedItem);
            Assert.Equal(-1, target.SelectedIndex);
        }
示例#27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ItemsControl"/> class.
 /// </summary>
 public ItemsControl()
 {
     Classes.Add(":empty");
     Items = new PerspexList<object>();
 }
        public void Removing_Selected_Item_Should_Clear_Selection()
        {
            var items = new PerspexList<Item>
            {
                new Item(),
                new Item(),
            };

            var target = new SelectingItemsControl
            {
                Items = items,
                Template = this.Template(),
            };

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

            Assert.Equal(items[1], target.SelectedItem);
            Assert.Equal(1, target.SelectedIndex);

            items.RemoveAt(1);

            Assert.Equal(null, target.SelectedItem);
            Assert.Equal(-1, target.SelectedIndex);
        }
示例#29
0
文件: Node.cs 项目: vebin/Perspex
 public Node()
 {
     Children = new PerspexList <Node>();
 }
示例#30
0
文件: Visual.cs 项目: KvanTTT/Perspex
        /// <summary>
        /// Initializes a new instance of the <see cref="Visual"/> class.
        /// </summary>
        public Visual()
        {
            _visualLogger = Log.ForContext(new[]
            {
                new PropertyEnricher("Area", "Visual"),
                new PropertyEnricher("SourceContext", GetType()),
                new PropertyEnricher("Id", GetHashCode()),
            });

            var visualChildren = new PerspexList<IVisual>();
            visualChildren.ResetBehavior = ResetBehavior.Remove;
            visualChildren.Validate = ValidateLogicalChild;
            visualChildren.CollectionChanged += VisualChildrenChanged;
            VisualChildren = visualChildren;
        }
示例#31
0
        public void RemoveAll_With_Null_Should_Throw_Exception()
        {
            var target = new PerspexList <int>();

            Assert.Throws <ArgumentNullException>(() => target.RemoveAll(null));
        }