在ListView中使用Drag和Drop的多个事件

时间:2016-01-19 15:17:56

标签: c# wpf listview drag-and-drop

我有WPF的简单ListView申请。

我添加了通过ListView将文件添加到Drag

的选项
ListView lv;

private void lv_Drop(object sender, DragEventArgs e)
{
    e.Effects = DragDropEffects.All;
}

private void lv_DragEnter(object sender, DragEventArgs e)
{
    try
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        FileAttributes attr = File.GetAttributes(files[0]);
        AddFiles(files);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error");
    }
}

private void AddFiles(string[] files)
{
   // Add the files into my `ListView`.
}

问题是Drag ListView一个文件进入我的@Component public class CustomLogoutHandler implements LogoutHandler { @Autowired private SessionRegistry sessionRegistry; @Override public void logout(HttpServletRequest httpServletRequest, httpServletResponse httpServletResponse, Authentication authentication) { ... httpServletRequest.getSession().invalidate(); httpServletResponse.setStatus(HttpServletResponse.SC_OK); //redirect to login httpServletResponse.sendRedirect("/"); List<SessionInformation> userSessions = sessionRegistry.getAllSessions(user, true); for (SessionInformation session: userSessions) { sessionRegistry.removeSessionInformation(session.getSessionId()); } } @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public SessionRegistry sessionRegistry() { if (sessionRegistry == null) { sessionRegistry = new SessionRegistryImpl(); } return sessionRegistry; } @Bean public static ServletListenerRegistrationBean httpSessionEventPublisher() { return new ServletListenerRegistrationBean(new HttpSessionEventPublisher()); } @Bean public ConcurrentSessionControlAuthenticationStrategy concurrentSessionControlAuthenticationStrategy() { ConcurrentSessionControlAuthenticationStrategy strategy = new ConcurrentSessionControlAuthenticationStrategy(sessionRegistry()); strategy.setExceptionIfMaximumExceeded(true); strategy.setMessageSource(messageSource); return strategy; } } 后触发事件的次数超过几次,因此相同的文件多次添加到列表中。 这种行为会发生什么?

1 个答案:

答案 0 :(得分:0)

您不应该使用DragEnter事件。此事件&#34;在将对象拖入控件的边界时发生。&#34; 请参阅here

您正在寻找的是DragDrop-Event,可以找到here