【发布时间】:2020-05-20 17:26:11
【问题描述】:
通过 MacDonald 的“Pro WPF 4.5 in C#”学习 WPF,重点关注第 5 章,事件。 我将如何编写一个可与标签和文本框一起使用的通用事件处理程序来处理初始化拖放过程的 MouseDown 事件? 这是我的 TextBox 处理程序:
private void tBSource_MouseDown(object sender, EventArgs e) {
TextBox tBox = (TextBox)sender;
DragDrop.DoDragDrop(tBox, tBox.Text, DragDropEffects.Copy);
}
还有我的标签处理程序:
private void lblSource_MouseDown(object sender, EventArgs e) {
Label lbl = (Label)sender;
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);
}
如您所见,我使用的是 Content 属性和 Text 属性,具体取决于启动事件的对象。如果我尝试对两个发件人使用相同的属性,我会收到构建错误(无论我使用哪个)。如果我能避免重复,我会很高兴。我应该将条件分块到另一个函数中并在处理程序中调用它以确定应该使用什么属性?
【问题讨论】: