2

我正在开发一个 WPF 应用程序datagrid。在我的应用程序中,我创建了一个上下文菜单来隐藏和取消隐藏 datagrid 的列标题,同时将 datagrid 的 itemsource 分配给 IEnumerable 集合。

 this.dataGrid1.ItemsSource = objref.Result;


                grid_data = objref.Result;
                cxMenu = new ContextMenu();

                foreach (Microsoft.Windows.Controls.DataGridColumn item in dataGrid1.Columns)
                {
                    menuItem = new MenuItem();
                    menuItem.Header = item.Header;
                    menuItem.IsChecked = true;
                    cxMenu.Items.Add(menuItem);
                    menuItem.Click += new RoutedEventHandler(menuItem_Click);
                    menuItem.Checked += new RoutedEventHandler(menuItem_Checked);
                    menuItem.Unchecked += new RoutedEventHandler(menuItem_Unchecked);
                }

一切正常。当我取消选中列时已成功删除,但是当我再次检查我的列时MenuItemContextMenu它没有被添加。

我的检查事件的处理程序如下。

 void menuItem_Checked(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        dataGrid1.ItemsSource = null;
        dataGrid1.ItemsSource = objref.Result;// Again assgining the whole set to itemssource
        List<string> menuList = new List<string>();
        menuList.Clear();



    foreach (MenuItem menuItem in cxMenu.Items)
    {
        if (menuItem.IsChecked == false)
        {
            menuList.Add(menuItem.Header.ToString());
        }
    }

        Functionsclass objref = new Functionsclass();


        foreach (string menuItem in menuList)
        {
            foreach (Microsoft.Windows.Controls.DataGridColumn column in dataGrid1.Columns)
            {
                if (column.Header.ToString() == menuItem)
                {
                    dataGrid1.Columns.Remove(column);
                    break;
                }
            }
        }
    }

但是当我再次检查时,我的专栏没有添加。请帮助我。

更新 2:

 void menuItem_Click(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        if (item.IsChecked)
        {
            item.IsChecked = false;
        }
        else
        {
            item.IsChecked = true;
        }
    }

    void menuItem_Unchecked(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        foreach (Microsoft.Windows.Controls.DataGridColumn column in dataGrid1.Columns)
        {
            if (column.Header.ToString().Contains(item.Header.ToString()))
            {
                dataGrid1.Columns.Remove(column);
                break;
            }
        }
    }

取消选中处理程序。

4

2 回答 2

2

如果您只想隐藏/显示列,我认为删除/添加列不是正确的方法。我建议你利用Visibility列的属性。将其设置Visibility.Collapsed为隐藏它,然后Visibility.Visible使其再次可见。

column.Visibility = Visibility.Collapsed; // Column is hidden

column.Visibility = Visibility.Visible; //Column is Visible
于 2013-01-23T10:32:26.100 回答
1

我刚刚更改了我的选中和未选中事件处理程序,如下所示。现在它工作正常.. :)

//Unchecked handler

 void menuItem_Unchecked(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        foreach (Microsoft.Windows.Controls.DataGridColumn column in dataGrid1.Columns)
        {
           if (column.Header.ToString().Contains(item.Header.ToString()))
            {
               column.Visibility = Visibility.Collapsed;
                break;
            }


        }
    }

// 检查处理程序

    void menuItem_Checked(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;

        List<string> menuList = new List<string>();
        menuList.Clear();
         foreach (Microsoft.Windows.Controls.DataGridColumn column in dataGrid1.Columns)
        {
           if (column.Header.ToString().Contains(item.Header.ToString()))
            {
               column.Visibility = Visibility.Visible;
                break;
            }


        }

    }
于 2013-01-23T11:02:05.257 回答