Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Windows.Forms将代码转换为WPF_C#_.net_Wpf_Xaml_Datagridview - Fatal编程技术网

C#Windows.Forms将代码转换为WPF

C#Windows.Forms将代码转换为WPF,c#,.net,wpf,xaml,datagridview,C#,.net,Wpf,Xaml,Datagridview,我正在尝试将我的Windows窗体程序转换为WPF程序,以便在我的NotifyIcon系统托盘图标中获取图像。但我在转换它时遇到了问题。我当前的程序有一个tabcontrol1,它使用函数:TabPages和DrawItem。WPF没有这些函数,但它没有TabPages,而是有项目,但我不知道WPF的“DrawItem”应该改成什么 我在Windows窗体中使用“DrawItem”的原因是更改选项卡文本的颜色 更改自: if (!find<T>(p.Value))

我正在尝试将我的Windows窗体程序转换为WPF程序,以便在我的NotifyIcon系统托盘图标中获取图像。但我在转换它时遇到了问题。我当前的程序有一个tabcontrol1,它使用函数:TabPages和DrawItem。WPF没有这些函数,但它没有TabPages,而是有项目,但我不知道WPF的“DrawItem”应该改成什么

我在Windows窗体中使用“DrawItem”的原因是更改选项卡文本的颜色

更改自:

      if (!find<T>(p.Value))
      {
         T myPage = new T();
         myPage.Text = "Ping";
         this.FormstabControl.TabPages.Add(myPage);
         this.FormstabControl.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);                            
         myPage.DataGridView.DataSource = p.Result;
      }

在WPF中,您应该直接用XAML编写大部分GUI。WPF的全部功能是,您可以将(几乎)所有GUI创建抽象到XAML中。请问您为什么要更改文本的颜色?如果要禁用它,只需使用
enabled=“false”
。这将很困难,因为我80%的代码已经存在于Windows.Forms中(WinForms和WPF根本不兼容…您无法在尝试转换代码时逐行转换代码。如果有人正确创建了一个WPF应用程序,看起来像WinForms应用程序的精确副本,您可能无法识别其中的多个代码段。即使您真的做到了这一点,您最终也会发现由于WPF应用程序编写得很糟糕,没有充分利用WPF提供的功能。糟糕,值得一试。谢谢。@user2216791这就是为什么我一直不鼓励人们使用winforms的原因,winforms是一种完全无用且不受欢迎的技术。如果您当前的应用程序是WPF,您可以在许多其他技术中重复使用它(WinRT XAML、Silverlight、Windows Phone)进行了很少的更改。即使您想转移到一个完全不同的框架(如Android)实际上,您可以重用部分代码,因为WPF鼓励将UI和应用程序/表示逻辑分离。但事实并非如此,您现在只能使用不可移植、不可扩展的无用winforms代码。这真是令人伤心=(
      if (!find<T>(p.Value))
      {
         T myPage = new T();
         myPage.Text = "Ping";
         this.WPFtabControl.Items.Add(myPage);
         //this.WPFtabControl.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);                            
         myPage.DataGridView.DataSource = p.Result;
       }
    private bool find<T>()
    {
        bool found = false;
        System.Windows.Forms.TabControl FormstabControl= new System.Windows.Forms.TabControl();
        FormstabControl.TabPages.Add(this.WPFtabControl.Items.ToString());
        foreach (TabPage page in FormstabControl.TabPages)
        {
            if (page is T)
            {
                found = true;
                break;
            }
        }
        return found;
    }

    private bool find<T>(string text) where T : TabPage
    {
        bool found = false;
        System.Windows.Forms.TabControl FormstabControl= new System.Windows.Forms.TabControl();
        FormstabControl.TabPages.Add(this.WPFtabControl.Items.ToString());
        foreach (TabPage page in FormstabControl.TabPages)
        {
            if (page is T && text.Equals(page.Text))
            {
                found = true;
                break;
            }
        }
        return found;
    }
  this.notifyIcon.ContextMenu = new ContextMenu();
  this.notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Hide", new EventHandler(hideApp)));
  this.notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Show", new EventHandler(showApp)));
  this.notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Exit", new EventHandler(exitApp)));