public async Task Check_ItemContainerStyle_TextBlock() { var containerStyle = (Style)XamlReader.Load( @"<Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' TargetType='TextBlock'> <Setter Property='Foreground' Value='Green'/> </Style>" ); var itemStyle = (Style)XamlReader.Load( @"<Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' TargetType='TextBlock'> <Setter Property='Foreground' Value='Red'/> </Style>" ); var source = new[] { new TextBlock { Text = "First" }, new TextBlock { Text = "Second", Style = itemStyle }, new TextBlock { Text = "Third" }, }; var SUT = new ItemsControl() { ItemContainerStyle = containerStyle, ItemsSource = source, }; WindowHelper.WindowContent = SUT; await WindowHelper.WaitForIdle(); TextBlock firstTb = null; await WindowHelper.WaitFor(() => (firstTb = SUT.ContainerFromItem(source[0]) as TextBlock) != null); TextBlock secondTb = null; await WindowHelper.WaitFor(() => (secondTb = SUT.ContainerFromItem(source[1]) as TextBlock) != null); TextBlock thirdTb = null; await WindowHelper.WaitFor(() => (thirdTb = SUT.ContainerFromItem(source[2]) as TextBlock) != null); Assert.AreEqual(firstTb.Style, containerStyle); Assert.AreEqual(secondTb.Style, itemStyle); Assert.AreEqual(thirdTb.Style, containerStyle); }
public async Task When_ContainerSet_Then_ContentShouldBeSet() { var resources = new TestsResources(); var SUT = new ItemsControl { ItemTemplate = TextBlockItemTemplate }; WindowHelper.WindowContent = SUT; await WindowHelper.WaitForIdle(); var source = new[] { "item 0", }; SUT.ItemsSource = source; ContentPresenter cp = null; await WindowHelper.WaitFor(() => (cp = SUT.ContainerFromItem(source[0]) as ContentPresenter) != null); Assert.AreEqual("item 0", cp.Content); var tb = cp.FindFirstChild <TextBlock>(); Assert.IsNotNull(tb); Assert.AreEqual("item 0", tb.Text); }
public async Task When_NoItemTemplate() { var SUT = new ItemsControl() { ItemTemplate = null, ItemTemplateSelector = null, }; WindowHelper.WindowContent = SUT; await WindowHelper.WaitForIdle(); var source = new[] { "Item 1" }; SUT.ItemsSource = source; ContentPresenter cp = null; await WindowHelper.WaitFor(() => (cp = SUT.ContainerFromItem(source[0]) as ContentPresenter) != null); Assert.AreEqual("Item 1", cp.Content); var tb = cp.FindFirstChild <TextBlock>(); Assert.IsNotNull(tb); Assert.AreEqual("Item 1", tb.Text); }
public static void RegisterImplicitAnimations(this ItemsControl itemsControl) { // Check if SDK > 14393 if (!ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 3)) { return; } var compositor = ElementCompositionPreview.GetElementVisual(itemsControl).Compositor; // Create ImplicitAnimations Collection. var elementImplicitAnimation = compositor.CreateImplicitAnimationCollection(); // Define trigger and animation that should play when the trigger is triggered. elementImplicitAnimation["Offset"] = CreateOffsetAnimation(compositor); foreach (var item in itemsControl.Items) { var container = itemsControl.ContainerFromItem(item); if (container != null) // Item not rendered. { var elementVisual = ElementCompositionPreview.GetElementVisual((SelectorItem)container); elementVisual.ImplicitAnimations = elementImplicitAnimation; } } }
public async Task When_Collection_Reset() { var count = 0; var panel = new StackPanel(); var SUT = new ItemsControl() { ItemsPanelRoot = panel, ItemContainerStyle = BuildBasicContainerStyle(), InternalItemsPanelRoot = panel, ItemTemplate = new DataTemplate(() => { count++; return(new Border()); }) }; SUT.ApplyTemplate(); var c = new Helpers.ObservableCollectionEx <string>(); c.Add("One"); c.Add("Two"); c.Add("Three"); SUT.ItemsSource = c; Assert.AreEqual(count, 3); Assert.AreEqual(SUT.Items.Count, 3); using (c.BatchUpdate()) { c.Add("Four"); c.Add("Five"); } Assert.AreEqual(SUT.Items.Count, 5); Assert.AreEqual(count, 5); Assert.IsNotNull(SUT.ContainerFromItem("One")); Assert.IsNotNull(SUT.ContainerFromItem("Four")); Assert.IsNotNull(SUT.ContainerFromItem("Five")); }
public async Task When_NestedItemsControl_RecycleTemplate() { var template = (DataTemplate)XamlReader.Load(@" <DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <ItemsControl ItemsSource='{Binding NestedSource}' BorderBrush='Black' BorderThickness='1'> <ItemsControl.ItemTemplate> <DataTemplate> <Rectangle Fill='Red' Height='50' Width='50' /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate> " .Replace('\'', '"')); var initialSource = new object[] { new { NestedSource = new object[1] }, }; var resetSource = new object[] { new { NestedSource = new object[0] }, }; var SUT = new ItemsControl() { ItemsSource = initialSource, ItemTemplate = template, }; WindowHelper.WindowContent = SUT; var item = default(ContentPresenter); // [initial stage]: load the nested ItemsControl with items, so it has an initial height await WindowHelper.WaitForLoaded(SUT); await WindowHelper.WaitFor(() => (item = SUT.ContainerFromItem(initialSource[0]) as ContentPresenter) != null, message : "initial state: failed to find the item"); Assert.AreEqual(50, item.ActualHeight, delta: 1.0, "initial state: expecting the item to have an starting height of 50"); // [reset stage]: ItemsSource is reset with empty NestedSource, and we expected the height to be RE-measured SUT.ItemsSource = resetSource; await WindowHelper.WaitForIdle(); await WindowHelper.WaitFor(() => (item = SUT.ContainerFromItem(resetSource[0]) as ContentPresenter) != null, message : "reset state: failed to find the item"); Assert.AreEqual(0, item.ActualHeight, "reset state: expecting the item's height to be remeasured to 0"); }
public async Task When_SpecifyingDisplayMemberPath_Then_ContentShouldBeSet() { var resources = new TestsResources(); var SUT = new ItemsControl { ItemTemplate = TextBlockItemTemplate, DisplayMemberPath = "DisplayName" }; WindowHelper.WindowContent = SUT; await WindowHelper.WaitForIdle(); var source = new[] { new ItemForDisplayMemberPath { DisplayName = "item 0" }, new ItemForDisplayMemberPath { DisplayName = "item 1" }, new ItemForDisplayMemberPath { DisplayName = "item 2" } }; SUT.ItemsSource = source; async Task Assert(int index, string s) { ContentPresenter cp = null; await WindowHelper.WaitFor(() => (cp = SUT.ContainerFromItem(source[index]) as ContentPresenter) != null); cp.Content.Should().Be(s, $"ContainerFromItem() at index {index}"); var tb = cp.FindFirstChild <TextBlock>(); tb.Should().NotBeNull($"Item at index {index}"); tb.Text.Should().Be(s, $"TextBlock.Text at index {index}"); } using var _ = new AssertionScope(); await Assert(0, "item 0"); await Assert(1, "item 1"); await Assert(2, "item 2"); }
public async Task When_IsItsOwnItemContainer_FromSource() { var SUT = new ItemsControl(); WindowHelper.WindowContent = SUT; await WindowHelper.WaitForIdle(); var source = new FrameworkElement[] { new TextBlock { Text = "item 1" }, new ContentControl { Content = "item 2" }, new Ellipse() { Fill = new SolidColorBrush(Colors.HotPink), Width = 50, Height = 50 } }; SUT.ItemsSource = source; TextBlock tb = null; await WindowHelper.WaitFor(() => (tb = SUT.ContainerFromItem(source[0]) as TextBlock) != null); Assert.AreEqual("item 1", tb.Text); SUT.ItemsSource = source; ContentControl cc = null; await WindowHelper.WaitFor(() => (cc = SUT.ContainerFromItem(source[1]) as ContentControl) != null); Assert.AreEqual("item 2", cc.Content); await WindowHelper.WaitFor(() => (SUT.ContainerFromItem(source[2]) as Ellipse) != null); }
public static void RegisterBoundImplicitAnimations(this ItemsControl itemsControl) { var compositor = ElementCompositionPreview.GetElementVisual(itemsControl as UIElement).Compositor; // Create ImplicitAnimations Collection. var elementImplicitAnimation = compositor.CreateImplicitAnimationCollection(); // Define trigger and animation that should play when the trigger is triggered. elementImplicitAnimation["Offset"] = CreateOffsetAnimation(compositor); foreach (var item in itemsControl.Items) { var container = itemsControl.ContainerFromItem(item); var elementVisual = ElementCompositionPreview.GetElementVisual((SelectorItem)container); elementVisual.ImplicitAnimations = elementImplicitAnimation; } }
private static void OnAnimationSelectorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { // Make sure property is set to the children as well: ItemsControl itemsControl = sender as ItemsControl; if (itemsControl != null) { foreach (object item in itemsControl.Items) { UIElement container = itemsControl.ContainerFromItem(item) as UIElement; if (container != null) { SetAnimationSelector(container, e.NewValue as AnimationSelectorBase); } } } }
public void UpdateExternalSelection() { _cancelOnSelectionChanged = true; if (Items != null) { BarItems.Clear(); foreach (var item in Items.Where(r => r.IsSelected)) { BarItems.Add(item); } int selectedCount = Items.Count(r => r.IsSelected); if (selectedCount > 0) { // Set SelectionMode = Multiple before selecting items SelectionMode = ListViewSelectionMode.Multiple; if (selectedCount < Items.Count) { foreach (var item in Items) { if (ItemsControl.ContainerFromItem(item) is GridViewItem container) { container.IsSelected = item.IsSelected; } } } else { SelecteAll(); } } else { Mode = GridCommandBarMode.Idle; DeselectAll(); } UpdateCommandBar(); } _cancelOnSelectionChanged = false; }
public async Task Check_ItemContainerStyle_ContentControl() { var containerStyle = (Style)XamlReader.Load( @"<Style xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" TargetType=""ContentControl""> <Setter Property=""Foreground"" Value=""Red""/> <Setter Property=""Template""> <Setter.Value> <ControlTemplate TargetType=""ContentControl""> <ContentPresenter Content=""{TemplateBinding Content}"" Foreground=""{TemplateBinding Foreground}"" ContentTemplate=""{TemplateBinding ContentTemplate}"" /> </ControlTemplate> </Setter.Value> </Setter> </Style>" ); var itemStyle = (Style)XamlReader.Load( @"<Style xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" TargetType=""ContentControl""> <Setter Property=""Foreground"" Value=""Green""/> <Setter Property=""Template""> <Setter.Value> <ControlTemplate TargetType=""ContentControl""> <ContentPresenter Content=""{TemplateBinding Content}"" Foreground=""{TemplateBinding Foreground}"" ContentTemplate=""{TemplateBinding ContentTemplate}"" /> </ControlTemplate> </Setter.Value> </Setter> </Style>" ); var source = new[] { new ContentControl { Content = "First" }, new ContentControl { Content = "Second", Style = itemStyle }, new ContentControl { Content = "Third" }, }; var SUT = new ItemsControl() { ItemContainerStyle = containerStyle, ItemsSource = source, }; WindowHelper.WindowContent = SUT; await WindowHelper.WaitForIdle(); ContentControl first = null; await WindowHelper.WaitFor(() => (first = SUT.ContainerFromItem(source[0]) as ContentControl) != null); ContentControl second = null; await WindowHelper.WaitFor(() => (second = SUT.ContainerFromItem(source[1]) as ContentControl) != null); ContentControl third = null; await WindowHelper.WaitFor(() => (third = SUT.ContainerFromItem(source[2]) as ContentControl) != null); Assert.AreEqual(first.Style, containerStyle); Assert.AreEqual(second.Style, itemStyle); Assert.AreEqual(third.Style, containerStyle); }