WPF的ListView的简单使用

1.界面代码


                
                    
                        
                        
                        
                        
                    
                
                
                    
                
                
                    
                        
                        
                    
                
            

2.后台代码

//声明变量,将界面ListView与List绑定
private static ObservableCollection List = new ObservableCollection();

//ListView双击事件
private void lvUsers_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lvUsers.SelectedIndex == -1)
            {
                User user = new User();
                user.Desc = "复位";
                user.Axis = "焦距";
                user.Action = "复位";
                user.Position = 0;
                ActionsWnd actionsWnd = new ActionsWnd(user);
                if (true == actionsWnd.ShowDialog())
                {
                    //List新增,则在界面也会新增
                    List.Add(actionsWnd.WndUser);
                }
            }
            else
            {
                Console.WriteLine("11");
                var user = lvUsers.SelectedItem as User;

                ActionsWnd actionsWnd = new ActionsWnd(user);
                if (true == actionsWnd.ShowDialog())
                {
                    int nSelect = lvUsers.SelectedIndex;
                    List[nSelect] = actionsWnd.WndUser;
                }
            }
        }

//ListView添加的右键菜单事件
private void MenuItem1_Click(object sender, RoutedEventArgs e)
{
   int nIndex = lvUsers.SelectedIndex;
   if(nIndex >-1)
   {
       List.RemoveAt(nIndex);
   }
}

private void MenuItem2_Click(object sender, RoutedEventArgs e)
{
    List.Clear();
}

你可能感兴趣的:(WPF,wpf)