1

我正在为桌面/移动应用程序编写的 VS2005 中用 C# 编写的旧程序编辑。

在移动应用程序上,我仅限于使用 .NET Compact Framework 3.1,因此我发现很难实现通常非常简单的事情。

我希望树形菜单的某些区域变灰/禁用。这背后的原因是客户端可以拥有三个不同版本的应用程序之一;精简版、完整版和专业版。

如果客户使用的是产品的精简版,我希望精简版缺少的选项变灰,这样客户就可以看到他们缺少的东西。目前,我仅限于简单地将不可用的选项设置为对他们不可见。

简而言之,有没有一种方法可以在 .NET 紧凑框架 3.1 的约束下实现这一目标?

谢谢。

4

1 回答 1

2

您可以从 TreeView 和 TreeNode 实现派生类并实现这种行为。

   using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmartDeviceProject3
{
    public partial class Form1 : Form
    {

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.MainMenu mainMenu1;
        private TreeViewEx treeView1;

        public Form1()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.treeView1 = new TreeViewEx();

            this.SuspendLayout();
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(240, 268);
            this.treeView1.TabIndex = 0;
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.treeView1);
            this.Menu = this.mainMenu1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            treeView1.Nodes.Add(new TreeViewNodeEx()
            {
                Text = "Enabled"
            });
            treeView1.Nodes.Add(new TreeViewNodeEx()
            {
                Text = "Disabled",
                Enabled = false,
            });
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

    }

    public class TreeViewEx 
        : TreeView
    {
        protected override void OnAfterSelect(TreeViewEventArgs e)
        {
            var node = e.Node as TreeViewNodeEx;
            if (node != null && !node.Enabled)
            {
                return;
            }
            base.OnAfterSelect(e);
        }
    }

    public class TreeViewNodeEx 
        : TreeNode
    {
        private bool _enabled = true;
        public bool Enabled
        {
            get
            {
                return _enabled;
            }
            set
            {
                if (_enabled != value)
                {
                    _enabled = value;
                    if (_enabled)
                    {
                        this.ForeColor = Color.Black;
                    }
                    else
                    {
                        this.ForeColor = Color.Gray;
                    }
                }
            }
        }

    }
}
于 2013-08-22T07:41:11.603 回答