C#发现texbox

问题描述:

我有这样的事情:C#发现texbox

<ListBox Height="456" Margin="30,113,0,0" x:Name="listBox1" Width="446" Background="Black"> 
      <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28" Orientation="Horizontal"> 
        <TextBlock Text="{Binding name}" FontSize="28" Padding="10" > 
         <toolkit:GestureService.GestureListener> 
          <toolkit:GestureListener 
           Hold="GestureListenerHold"  /> 
        </toolkit:GestureService.GestureListener> 

        </TextBlock> 
        <TextBlock Text="{Binding id}" FontSize="24" Padding="10" > 
         <toolkit:GestureService.GestureListener> 
          <toolkit:GestureListener 
           Hold="GestureListenerHold"  /> 
        </toolkit:GestureService.GestureListener> 
        </TextBlock> 
        <TextBlock Text="{Binding status}" FontSize="24" Padding="10"> 
         <toolkit:GestureService.GestureListener> 
          <toolkit:GestureListener 
           Hold="GestureListenerHold"  /> 
        </toolkit:GestureService.GestureListener> 
        </TextBlock> 
       </StackPanel> 


      </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

,并在我的应用我做的:

data = (List<Device>)serializer.Deserialize(stream); 
         this.listBox1.ItemsSource = data; 

上的每个文本块我有一个手势监听器应该提供用户可以选择更改'名称',所以当他们拿着文本块时,应用程序将他导航到填充表单的另一个页面。

我的问题是如何找到绑定'名称',当我点击并按住另一个texblock的texblock?

你可以使用LINQ到的VisualTree,我写了一个工具,它允许您导航可视化树:

http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/

首先,命名为TextBlock,以便它可以被唯一标识:

<TextBlock x:Name="NameText" Text="{Binding name}" FontSize="28" Padding="10" /> 

然后,当你的其他的TextBlocks的一个被窃听,你可以按如下发现:

// locate the parent stackpanel 
var parentStackPanel = tappedTextBlock.Ancestors().First() 

// locate the names TextBlock 
var nameTextBlock = parentStackPanel.Elements() 
            .Where(el => el.Name == "NameText").Single(); 

GestureListenerHold方法中投下sender