csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.7 KiB
157 lines
4.7 KiB
// Copyright (c) The Perspex Project. All rights reserved.
|
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|
|
|
using System.Linq;
|
|
using Perspex.Controls.Presenters;
|
|
using Perspex.Controls.Templates;
|
|
using Perspex.Input;
|
|
using Perspex.LogicalTree;
|
|
using Perspex.Styling;
|
|
using Xunit;
|
|
|
|
namespace Perspex.Controls.UnitTests
|
|
{
|
|
public class ListBoxTests
|
|
{
|
|
[Fact]
|
|
public void ListBoxItem_Containers_Should_Be_Generated()
|
|
{
|
|
var items = new[] { "Foo", "Bar", "Baz " };
|
|
var target = new ListBox
|
|
{
|
|
Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
Items = items,
|
|
};
|
|
|
|
target.ApplyTemplate();
|
|
|
|
var text = target.Presenter.Panel.Children
|
|
.OfType<ListBoxItem>()
|
|
.Do(x => x.Template = ListBoxItemTemplate())
|
|
.Do(x => x.ApplyTemplate())
|
|
.Select(x => x.Presenter.Child)
|
|
.OfType<TextBlock>()
|
|
.Select(x => x.Text)
|
|
.ToList();
|
|
|
|
Assert.Equal(items, text);
|
|
}
|
|
|
|
[Fact]
|
|
public void LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items()
|
|
{
|
|
var target = new ListBox
|
|
{
|
|
Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
Items = new[] { "Foo", "Bar", "Baz " },
|
|
};
|
|
|
|
target.ApplyTemplate();
|
|
|
|
Assert.Equal(3, target.GetLogicalChildren().Count());
|
|
|
|
foreach (var child in target.GetLogicalChildren())
|
|
{
|
|
Assert.IsType<ListBoxItem>(child);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
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 FuncControlTemplate(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);
|
|
}
|
|
|
|
[Fact]
|
|
public void Setting_SelectedItem_Should_Set_Panel_Keyboard_Navigation()
|
|
{
|
|
var target = new ListBox
|
|
{
|
|
Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
Items = new[] { "Foo", "Bar", "Baz " },
|
|
};
|
|
|
|
target.ApplyTemplate();
|
|
|
|
target.Presenter.Panel.Children[1].RaiseEvent(new PointerPressEventArgs
|
|
{
|
|
RoutedEvent = InputElement.PointerPressedEvent,
|
|
MouseButton = MouseButton.Left,
|
|
});
|
|
|
|
var panel = target.Presenter.Panel;
|
|
|
|
Assert.Equal(
|
|
KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel),
|
|
panel.Children[1]);
|
|
}
|
|
|
|
private Control CreateListBoxTemplate(ITemplatedControl parent)
|
|
{
|
|
return new ScrollViewer
|
|
{
|
|
Template = new FuncControlTemplate(CreateScrollViewerTemplate),
|
|
Content = new ItemsPresenter
|
|
{
|
|
Name = "PART_ItemsPresenter",
|
|
[~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty),
|
|
}
|
|
};
|
|
}
|
|
|
|
private FuncControlTemplate ListBoxItemTemplate()
|
|
{
|
|
return new FuncControlTemplate<ListBoxItem>(parent => new ContentPresenter
|
|
{
|
|
Name = "PART_ContentPresenter",
|
|
[!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
|
|
});
|
|
}
|
|
|
|
private Control CreateScrollViewerTemplate(ITemplatedControl parent)
|
|
{
|
|
return new ScrollContentPresenter
|
|
{
|
|
[~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty),
|
|
};
|
|
}
|
|
|
|
private class Item
|
|
{
|
|
public Item(string value)
|
|
{
|
|
Value = value;
|
|
}
|
|
|
|
public string Value { get; }
|
|
}
|
|
}
|
|
}
|
|
|