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

            target.ApplyTemplate();

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

            foreach (var child in target.GetLogicalChildren())
            {
                Assert.IsType<ListBoxItem>(child);
            }
        }
示例#4
0
 /// <summary>
 /// The default template for a <see cref="ListBox"/> control.
 /// </summary>
 /// <param name="control">The control being styled.</param>
 /// <returns>The root of the instantiated template.</returns>
 public static Control Template(ListBox control)
 {
     return new Border
     {
         Padding = new Thickness(4),
         [~Border.BackgroundProperty] = control[~TemplatedControl.BackgroundProperty],
         [~Border.BorderBrushProperty] = control[~TemplatedControl.BorderBrushProperty],
         [~Border.BorderThicknessProperty] = control[~TemplatedControl.BorderThicknessProperty],
         Child = new ScrollViewer
         {
             Content = new ItemsPresenter
             {
                 Name = "itemsPresenter",
                 [~ItemsPresenter.ItemsProperty] = control[~ItemsControl.ItemsProperty],
                 [~ItemsPresenter.ItemsPanelProperty] = control[~ItemsControl.ItemsPanelProperty],
             }
         }
     };
 }
示例#5
0
        private static TabItem ListsTab()
        {
            ListBox listBox;

            return new TabItem
            {
                Header = "Lists",
                Content = new StackPanel
                {
                    DataTemplates = new DataTemplates
                    {
                        new DataTemplate<Item>(x =>
                            new StackPanel
                            {
                                Children = new Controls
                                {
                                    new TextBlock { Text = x.Name, FontSize = 24 },
                                    new TextBlock { Text = x.Value },
                                }
                            })
                    },
                    Orientation = Orientation.Horizontal,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Gap = 8,
                    Children = new Controls
                    {
                        new TreeView
                        {
                            Name = "treeView",
                            Items = s_treeData,
                        },
                        (listBox = new ListBox
                        {
                            Items = s_listBoxData,
                            MaxHeight = 300,
                        }),
                        new DropDown
                        {
                            Items = s_listBoxData,
                            SelectedItem = s_listBoxData[0],
                            VerticalAlignment = VerticalAlignment.Center,
                        }
                    }
                },
            };
        }