-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support DataTemplate is container host
- Loading branch information
1 parent
79a3444
commit 49c46f4
Showing
6 changed files
with
375 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...UI.Tests/Windows_UI_XAML_Controls/ListViewBaseTests/Given_ListViewBase_CustomContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Uno.Extensions; | ||
using Windows.Foundation; | ||
using Windows.Foundation.Collections; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Controls.Primitives; | ||
using Windows.UI.Xaml.Data; | ||
|
||
namespace Uno.UI.Tests.ItemsControlTests_CustomContainer | ||
{ | ||
[TestClass] | ||
public class Given_ListViewBase_CustomContainer | ||
{ | ||
[TestMethod] | ||
public void When_TemplateRootIsOwnContainer() | ||
{ | ||
var count = 0; | ||
var panel = new StackPanel(); | ||
|
||
var SUT = new MyItemsControl() | ||
{ | ||
ItemsPanelRoot = panel, | ||
ItemTemplate = new DataTemplate(() => | ||
{ | ||
count++; | ||
return new MyCustomItemContainer() { MyValue = 42 }; | ||
}), | ||
Template = new ControlTemplate(() => new ItemsPresenter()), | ||
}; | ||
|
||
SUT.ApplyTemplate(); | ||
|
||
SUT.ItemsSource = new object[] | ||
{ | ||
42 | ||
}; | ||
|
||
var container = SUT.ContainerFromIndex(0) as MyCustomItemContainer; | ||
|
||
Assert.IsNotNull(container); | ||
Assert.IsTrue(container.IsGeneratedContainer); | ||
Assert.IsFalse(container.ContentTemplateRoot is MyCustomItemContainer); | ||
Assert.AreEqual(42, container.Content); | ||
} | ||
|
||
[TestMethod] | ||
public void When_TemplateSelector_RootIsOwnContainer() | ||
{ | ||
var count = 0; | ||
var panel = new StackPanel(); | ||
|
||
var itemsPresenter = new MyItemsControl(); | ||
|
||
var itemTemplate = new DataTemplate(() => | ||
{ | ||
count++; | ||
return new MyCustomItemContainer() { MyValue = 42 }; | ||
}); | ||
|
||
var SUT = new MyItemsControl() | ||
{ | ||
ItemsPanelRoot = panel, | ||
ItemTemplateSelector = new MyDataTemplateSelector(i => itemTemplate), | ||
Template = new ControlTemplate(() => new ItemsPresenter()), | ||
}; | ||
|
||
SUT.ApplyTemplate(); | ||
|
||
SUT.ItemsSource = new object[] | ||
{ | ||
42 | ||
}; | ||
|
||
var container = SUT.ContainerFromIndex(0) as MyCustomItemContainer; | ||
|
||
Assert.IsTrue(container is MyCustomItemContainer); | ||
Assert.IsTrue(container.IsGeneratedContainer); | ||
Assert.IsFalse(container.ContentTemplateRoot is MyCustomItemContainer); | ||
Assert.AreEqual(42, container.Content); | ||
} | ||
} | ||
|
||
public class MyDataTemplateSelector : DataTemplateSelector | ||
{ | ||
private Func<object, DataTemplate> _selector; | ||
|
||
public MyDataTemplateSelector(Func<object, DataTemplate> selector) => _selector = selector; | ||
|
||
protected override DataTemplate SelectTemplateCore(object item) => _selector.Invoke(item); | ||
} | ||
|
||
|
||
public class MyItemsControl : ListView | ||
{ | ||
protected override DependencyObject GetContainerForItemOverride() | ||
=> new MyCustomItemContainer(); | ||
|
||
protected override bool IsItemItsOwnContainerOverride(object item) | ||
=> item is MyCustomItemContainer; | ||
} | ||
|
||
public class MyCustomItemContainer : SelectorItem | ||
{ | ||
public int MyValue | ||
{ | ||
get { return (int)GetValue(MyValueProperty); } | ||
set { SetValue(MyValueProperty, value); } | ||
} | ||
|
||
// Using a DependencyProperty as the backing store for MyValue. This enables animation, styling, binding, etc... | ||
public static readonly DependencyProperty MyValueProperty = | ||
DependencyProperty.Register("MyValue", typeof(int), typeof(MyCustomItemContainer), new PropertyMetadata(0)); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.