如何通过标签名称获取ListBox中的元素?

时间:2012-09-14 04:37:58

标签: windows-phone-7 windows-phone-7.1

  

可能重复:
  Is there any way to get XAML element by Tag property?

我有这样的代码:

                <ListBox x:Name="ImageListBox"  ScrollViewer.VerticalScrollBarVisibility="Disabled"
                     Height="100"  >

                    <ListBox.ItemsPanel >
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel  ItemHeight="100" ItemWidth="110" VerticalAlignment="Center" HorizontalAlignment="Center" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

                <ListBox.ItemTemplate>
                        <DataTemplate>
                        <Grid Tap="StackPanel_Tap" Tag="{Binding Type}" Name="Yashu">
                            <Border BorderThickness="{Binding Thickness}"  CornerRadius="0"   BorderBrush="White" Width="100" Height="100"  HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="1" >
                                <!--<Grid>-->
                                <Image Tag="{Binding Type}" Source="{Binding Location}" Opacity="1" Width="100"  Height="100" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center" UseLayoutRounding="True"  >
                                </Image>
                            </Border>

                        </Grid>
                    </DataTemplate>
                    </ListBox.ItemTemplate>
            </ListBox>
        </ScrollViewer>

如何通过标签名称获取ListBox中的网格元素?

1 个答案:

答案 0 :(得分:0)

在事件处理程序StackPanel_Tap中,将发件人转换为网格。

object tag = (sender as Grid).Tag;

并使用Grid grid1 = sender as Grid;

获取网格元素

注意:仅当用户点击某个项目时,此功能才有效。我希望它有所帮助