2013-07-07 49 views
0

內的子項我有一個列表ñ畫布,我需要找到在C#中的畫面內TextBlock的屬性,當它被點擊時,的.xaml代碼如下的Windows Phone - 尋找帆布

<Canvas Height="175" Width="360" Background="{StaticResource PhoneAccentBrush}" toolkit:TiltEffect.IsTiltEnabled="True" Name="canvasNote" Tap="canvasNote_Tap"> 
    <toolkit:ContextMenuService.ContextMenu> 
     <toolkit:ContextMenu IsZoomEnabled="False"> 
     <toolkit:MenuItem Header="Open" Click="hubTileOpen_Click"/> 
     <toolkit:MenuItem Header="Edit" Click="hubTileEdit_Click"/> 
     <toolkit:MenuItem Header="Delete" Click="hubTileDelete_Click"/> 
     </toolkit:ContextMenu> 
    </toolkit:ContextMenuService.ContextMenu> 
    <TextBlock Text="{Binding Title}" Margin="12,4,0,0" FontWeight="Bold" FontSize="20" Foreground="White"/> 
    <TextBlock Text="{Binding Details}" Margin="12,28,0,0" TextWrapping="Wrap" Width="352" FontSize="20" Foreground="White"/> 
</Canvas> 

任何想法?

所有幫助表示感謝!

回答

0

有幾種方法。一種方法是設置的TextBlocks的x:Name財產,並使用FindName方法檢索它們:

<Canvas Height="175" Width="360" Background="{StaticResource PhoneAccentBrush}" toolkit:TiltEffect.IsTiltEnabled="True" Name="canvasNote" Tap="canvasNote_Tap"> 
    <toolkit:ContextMenuService.ContextMenu> 
     <toolkit:ContextMenu IsZoomEnabled="False"> 
     <toolkit:MenuItem Header="Open" Click="hubTileOpen_Click"/> 
     <toolkit:MenuItem Header="Edit" Click="hubTileEdit_Click"/> 
     <toolkit:MenuItem Header="Delete" Click="hubTileDelete_Click"/> 
     </toolkit:ContextMenu> 
    </toolkit:ContextMenuService.ContextMenu> 
    <TextBlock x:Name="Title" Text="{Binding Title}" Margin="12,4,0,0" FontWeight="Bold" FontSize="20" Foreground="White"/> 
    <TextBlock x:Name="Details" Text="{Binding Details}" Margin="12,28,0,0" TextWrapping="Wrap" Width="352" FontSize="20" Foreground="White"/> 
</Canvas> 

然後在Tap事件:

private void canvasNote_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    var canvas = (Canvas)sender; 

    var title = (TextBlock)canvas.FindName("Title"); 

    System.Diagnostics.Debug.WriteLine(title.Text); 
} 

但由於您使用的ñ畫布如果不使用數據模板,您可能會遇到名稱衝突。因此,另一種方法是使用Tag屬性來代替:

<Canvas Height="175" Width="360" Background="{StaticResource PhoneAccentBrush}" toolkit:TiltEffect.IsTiltEnabled="True" Name="canvasNote" Tap="canvasNote_Tap"> 
    <toolkit:ContextMenuService.ContextMenu> 
     <toolkit:ContextMenu IsZoomEnabled="False"> 
     <toolkit:MenuItem Header="Open" Click="hubTileOpen_Click"/> 
     <toolkit:MenuItem Header="Edit" Click="hubTileEdit_Click"/> 
     <toolkit:MenuItem Header="Delete" Click="hubTileDelete_Click"/> 
     </toolkit:ContextMenu> 
    </toolkit:ContextMenuService.ContextMenu> 
    <TextBlock Tag="Title" Text="{Binding Title}" Margin="12,4,0,0" FontWeight="Bold" FontSize="20" Foreground="White"/> 
    <TextBlock Tag="Details" Text="{Binding Details}" Margin="12,28,0,0" TextWrapping="Wrap" Width="352" FontSize="20" Foreground="White"/> 
</Canvas> 

而在Tap事件:

private void canvasNote_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    var canvas = (Canvas)sender; 

    var title = canvas.Children.OfType<TextBlock>().First(t => (t.Tag as string) == "Title"); 

    System.Diagnostics.Debug.WriteLine(title.Text); 
} 
+0

對不起,我應該說;那不是代碼的完整部分,它是在一個帶有數據模板的列表框中。然而,你寫的第一部分工作就像一個魅力,非常感謝! – Newbie