首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

WPF C# 實現 Label MouseDown 事件顯示不同內容

     編輯:程式設計疑難解答

需求

兩個 Label 放第一行,鼠標按下時在第二行 RichTextBox 顯示不同內容。

實現

XAML 綁定事件:

<Label Content="標簽1" MouseDown="Label_MouseDown" Tag="內容1" />
<Label Content="標簽2" MouseDown="Label_MouseDown" Tag="內容2" />
<RichTextBox x:Name="rtb" />

後台代碼:

private void Label_MouseDown(object sender, MouseButtonEventArgs e) {
    var label = sender as Label;
    if (label != null) {
        rtb.Document.Blocks.Clear();
        rtb.AppendText(label.Tag.ToString());
    }
}

說明

  • 利用 Tag 屬性攜帶差異化數據。
  • sender 區分觸發源,MouseDown 為按下瞬間觸發。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved