How to change ItemsPanelTemplate WrapGrid from XAML code?

I am trying to change the MaximumRowsOrColumns property of my WrapGrid as follows:

<GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid x:Name="wrapGridItems" Orientation="Vertical" MaximumRowsOrColumns="1" /> </ItemsPanelTemplate> </GridView.ItemsPanel> 

And then I use this code to modify the WrapGrid:

 <VisualState x:Name="Snapped"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="wrapGridItems" Storyboard.TargetProperty="MaximumRowsOrColumns"> <DiscreteObjectKeyFrame KeyTime="0" Value="-1"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="headerText" Storyboard.TargetProperty="Text"> <DiscreteObjectKeyFrame KeyTime="0" Value="Pins"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> 

But I get an error

WinRT Info: Cannot resolve wrapGridItems TargetName.

How do I access the WrapGrid in the ObjectAnimationUsingKeyFrames Storyboard.TargetName property?

+6
source share
2 answers

You cannot access elements inside templates using x: Name. Since the template can be created multiple times, the animation will not be able to determine which element it should manipulate.

If you need to change the property of an element inside a template, you must use the binding:

 <GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid Orientation="Vertical" MaximumRowsOrColumns="{Binding MyMaxRowsOrCollumns}" /> </ItemsPanelTemplate> </GridView.ItemsPanel> 
+4
source

Project Code:

 <GridView > <GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid x:Name="wrapGrid" Orientation="Vertical" MaximumRowsOrColumns="{Binding MyMaxRowsOrCollumns}"></WrapGrid> </ItemsPanelTemplate> </GridView.ItemsPanel> </GridView > 

C # code:

Create Dependency Property

 public int MyMaxRowsOrCollumns { get { return (int)GetValue(MyMaxRowsOrCollumnsProperty); } set { SetValue(MyMaxRowsOrCollumnsProperty, value); } } // Using a DependencyProperty as the backing store for MyMaxRowsOrCollumns. This enables animation, styling, binding, etc... public static readonly DependencyProperty MyMaxRowsOrCollumnsProperty = DependencyProperty.Register("MyMaxRowsOrCollumns", typeof(int), typeof(DashBord), new PropertyMetadata(2)); 
0
source

Source: https://habr.com/ru/post/927614/


All Articles