MatthewSot
MatthewSot

Reputation: 3594

How to change ItemsPanelTemplate WrapGrid From XAML code?

I'm trying to modify the MaximumRowsOrColumns property of my WrapGrid like this:

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

And then I'm using this code to change 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'm getting the error

WinRT information: Cannot resolve TargetName wrapGridItems.

How should I refer to the WrapGrid in the ObjectAnimationUsingKeyFrames Storyboard.TargetName property?

Upvotes: 6

Views: 4504

Answers (2)

Samadhan
Samadhan

Reputation: 429

Design Code:

<GridView >

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

C# code:

Create dependancy 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));

Upvotes: 0

Denis
Denis

Reputation: 4115

You can not access elements inside templates using x:Name. Since template could be instantiated many times animation wouldn't be able to tell which element it should manipulate.

If you need to change property of element inside the template you should use binding:

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

Upvotes: 4

Related Questions