有幾種方法。一種方法是設置的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);
}
對不起,我應該說;那不是代碼的完整部分,它是在一個帶有數據模板的列表框中。然而,你寫的第一部分工作就像一個魅力,非常感謝! – Newbie