2012-09-14 154 views
-1

可能重複:
Is there any way to get XAML element by Tag property?如何通過其標籤名稱獲取ListBox中的元素?

我有這樣的代碼:

   <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中的網格元素?

回答

0

在事件處理程序StackPanel_Tap中,將發件人轉換爲網格。

object tag = (sender as Grid).Tag;

,並得到網格元素使用Grid grid1 = sender as Grid;

注:這僅當用戶點擊一個項目工作。我希望它可以幫助

+0

謝謝你,我已經做到了..我想預先選擇列表框中的項目 – Yashavantha

相關問題