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); }
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); }
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); } }