我在代码中添加了一个按钮列表,并订阅了他们的 mouseleave 事件。对于我使用匿名函数订阅事件的每个按钮,问题是当我运行应用程序时,它们都订阅了最后一个匿名函数。这是代码,我希望我自己解释一下。
var modules = ModulesSC.GetAllMoudules();
var imageSet = ModulesSC.GetModuleImageSet();
foreach (var module in modules)
{
var btn = new Button();
btn.SetResourceReference(Control.TemplateProperty, "SideMenuButton");
btn.Content = module.Title;
btn.MouseEnter += (s, e) => { ShowInfo(module.Description); };
btn.MouseLeave += (s, e) => { HideInfo(); };
ModuleButtons.Children.Add(btn);
}
protected void HideInfo()
{
DescriptionLabel.Visibility = Visibility.Collapsed;
DescriptionText.Text = string.Empty;
}
protected void ShowInfo(string description)
{
DescriptionLabel.Visibility = Visibility.Visible;
DescriptionText.Text = description;
}
当我运行应用程序时,它们都使用 las“module.Description”调用 showInfo
谢谢-亚历杭德罗