前台:
1 <Window.Resources> 2 <Style TargetType="{x:Type TextBlock}" x:Key="ClickableTextBlockStyle"> 3 <EventSetter Event="MouseDown" Handler="TextBlock_MouseDown"/> 4 </Style> 5 </Window.Resources> 6 7 <StackPanel> 8 9 10 <TextBlock Tag="1" Text="Customers" Style="{DynamicResource ResourceKey=ClickableTextBlockStyle}"/> 11 <TextBlock Tag="2" Text="Appointments" Style="{DynamicResource ResourceKey=ClickableTextBlockStyle}"/>
后台:
1 private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e) 2 { 3 int id = Int32.Parse(((TextBlock)sender).Tag.ToString()); 4 MessageBox.Show("you chose id "+id.ToString()); 5 }