在GridView中访问Grid以编程方式操作其宽度

时间:2016-07-20 13:48:34

标签: c# uwp

    <GridView x:Name="MainGridView">
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="viewModels:SomeViewModel">
                <Grid x:Name="I_NEED_THIS">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="40*"/>
                        <ColumnDefinition Width="60*"/>
                        <ColumnDefinition Width="180*"/>
                        <ColumnDefinition Width="40*"/>
                        <ColumnDefinition Width="40*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock with some binding/>
                    <TextBlock with some binding/>
                    <TextBlock with some binding/>
                    <TextBlock with some binding/>
                    <TextBlock with some binding/>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

我需要在运行时访问每个Grid(x:Name =&#34; I_NEED_THIS&#34;)以在运行时操作一些网格参数。我如何可以遍历创建的每个网格?

1 个答案:

答案 0 :(得分:1)

实际上,您必须遍历Visual Tree并找到您想要进行更改的元素。这篇博客文章可以帮助您入门:

http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html