如何从XAML代码更改ItemsPanelTemplate WrapGrid?

时间:2012-10-13 02:24:24

标签: c# xaml windows-8

我正在尝试修改我的WrapGrid的MaximumRowsOrColumns属性,如下所示:

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

然后我使用此代码更改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>

但我收到了错误

  

WinRT信息:无法解析TargetName wrapGridItems。

我应该如何在ObjectAnimationUsingKeyFrames Storyboard.TargetName属性中引用WrapGrid?

2 个答案:

答案 0 :(得分:4)

您无法使用x:Name访问模板内的元素。由于模板可以多次实例化,因此动画将无法分辨它应该操作哪个元素。

如果你需要更改模板中元素的属性,你应该使用绑定:

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

答案 1 :(得分:0)

设计代码:

<GridView >

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

C#代码:

创建依赖属性

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