如何在NavBarGroup中获取CheckEdit [英] How can I get a CheckEdit in a NavBarGroup

查看:215
本文介绍了如何在NavBarGroup中获取CheckEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在NavBarGroup的标题中放置一个复选框(及其标题)。有没有办法这样做?

We need to place a check box (and caption for it) in the header of a NavBarGroup. Is there a way to do this?

推荐答案

我们创建了一个从NavBarGroup继承的NavBarGroupChecked类(NavBarGroupChecked.cs)放下来取代它。它添加了一个RepositoryItemCheckEdit成员,跟踪该复选框并实现自定义绘图。它具有一个Checked属性,它告诉您是否被检查,并且当检查状态更改时将被调用的事件。几乎是这样

We created a NavBarGroupChecked class (NavBarGroupChecked.cs) that inherits from NavBarGroup and can just be dropped in to replace it. It adds a RepositoryItemCheckEdit member that tracks the checkbox and implements custom draw. It has a Checked property that tells you if it is checked and an event that will be called when the Checked status changes. That's pretty much it. Just drops in and works.

代码在下面,而且可在此下载

// built from http://www.devexpress.com/example=E2061

using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraNavBar;
using DevExpress.XtraNavBar.ViewInfo;

namespace AutoTagCore.net.windward.controls
{
    /// <summary>
    /// A NavBarGroup that has a check box (with caption) in its header.
    /// </summary>
    public class NavBarGroupChecked : NavBarGroup
    {

        /// <summary>
        /// Occurs when the Checked property value has been changed. 
        /// </summary>
        public event EventHandler CheckedChanged;

        private const int CHECK_BOX_WIDTH = 15;
        private bool isLocked;
        private RepositoryItemCheckEdit _GroupEdit;
        private NavBarControl _NavBarControl;
        private Rectangle hotRectangle;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:DevExpress.XtraNavBar.NavBarGroup"/> class, with the specified caption.
        /// </summary>
        /// <param name="caption">A string representing the NavBar group's caption.</param>
        public NavBarGroupChecked(string caption)
            : base(caption)
        {
            ctor();
        }

        private void ctor()
        {
            GroupEdit = new RepositoryItemCheckEdit { GlyphAlignment = DevExpress.Utils.HorzAlignment.Far };
            GroupEdit.Appearance.Options.UseTextOptions = true;
            GroupEdit.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            GroupEdit.GlyphAlignment = DevExpress.Utils.HorzAlignment.Far;
            ItemChanged += NavBarGroupChecked_ItemChanged;
        }

        private void NavBarGroupChecked_ItemChanged(object sender, System.EventArgs e)
        {
            if (NavBar != NavBarControl)
                NavBarControl = NavBar;
        } 


        /// <summary>
        /// Creates an instance of the <see cref="T:DevExpress.XtraNavBar.NavBarGroup"/> class.
        /// </summary>
        public NavBarGroupChecked()
        {
            ctor();
        }

        /// <summary>
        /// The NavBarControl that owns this. This must be set to work.
        /// </summary>
        private NavBarControl NavBarControl
        {
            get { return _NavBarControl; }
            set { UnsubscribeEvents(value); _NavBarControl = value; SubscribeEvents(value); }
        }

        private void SubscribeEvents(NavBarControl navBarControl)
        {
            if (navBarControl == null)
                return;
            NavBarControl.CustomDrawGroupCaption += NavBarControl_CustomDrawGroupCaption;
            NavBarControl.MouseClick += NavBarControl_MouseClick;
        }

        private void UnsubscribeEvents(NavBarControl navBarControl)
        {
            if (navBarControl != null)
                return;
            NavBarControl.CustomDrawGroupCaption -= NavBarControl_CustomDrawGroupCaption;
            NavBarControl.MouseClick -= NavBarControl_MouseClick;
        }

        /// <summary>
        /// true if the box is checked.
        /// </summary>
        public bool Checked { get; set; }

        /// <summary>
        /// The indent of the check box for the end of the header.
        /// </summary>
        public int CheckIndent { get; set; }

        ///<summary>
        /// The check box displayed in the header.
        ///</summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RepositoryItemCheckEdit GroupEdit
        {
            get { return _GroupEdit; }
            set { _GroupEdit = value; }
        }

        private Rectangle GetCheckBoxBounds(Rectangle fixedCaptionBounds)
        {
            return new Rectangle(fixedCaptionBounds.Right - CHECK_BOX_WIDTH - CheckIndent, fixedCaptionBounds.Top, CHECK_BOX_WIDTH, fixedCaptionBounds.Height); 
        }

        private bool IsCustomDrawNeeded(NavBarGroup group)
        {
            return GroupEdit != null && NavBarControl != null && !isLocked && group == this;
        }

        private void NavBarControl_CustomDrawGroupCaption(object sender, CustomDrawNavBarElementEventArgs e)
        {
            NavGroupInfoArgs infoArgs = (NavGroupInfoArgs) e.ObjectInfo;
            if (!IsCustomDrawNeeded(infoArgs.Group))
                return;
            try
            {
                isLocked = true;
                BaseNavGroupPainter painter = NavBarControl.View.CreateGroupPainter(NavBarControl);
                Rectangle checkBoxBounds = GetCheckBoxBounds(infoArgs.CaptionBounds);
                painter.DrawObject(infoArgs);
                DrawCheckBox(e.Graphics, checkBoxBounds);
                e.Handled = true;
            }
            finally
            {
                isLocked = false;
            }
        }

        private void DrawCheckBox(Graphics g, Rectangle r)
        {
            BaseEditPainter painter = GroupEdit.CreatePainter();
            BaseEditViewInfo info = GroupEdit.CreateViewInfo();
            info.EditValue = Checked;
            SizeF textBounds = info.Appearance.CalcTextSize(g, GroupEdit.Caption, 500);
            int totalWidth = (int)textBounds.Width + r.Width + 10;
            info.Bounds = new Rectangle(r.Right - totalWidth, r.Y, totalWidth, r.Height);
            info.CalcViewInfo(g);
            ControlGraphicsInfoArgs args = new ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
            painter.Draw(args);
            args.Cache.Dispose();
        }

        private static NavBarViewInfo GetNavBarView(NavBarControl NavBar)
        {
            PropertyInfo pi = typeof(NavBarControl).GetProperty("ViewInfo", BindingFlags.Instance | BindingFlags.NonPublic);
            return pi.GetValue(NavBar, null) as NavBarViewInfo;
        }

        private bool IsCheckBox(Point p)
        {
            NavBarHitInfo hi = NavBarControl.CalcHitInfo(p);
            if (hi.Group == null || hi.Group != this)
                return false;
            NavBarViewInfo vi = GetNavBarView(NavBarControl);
            vi.Calc(NavBarControl.ClientRectangle);
            NavGroupInfoArgs groupInfo = vi.GetGroupInfo(hi.Group);
            Rectangle checkBounds = GetCheckBoxBounds(groupInfo.CaptionBounds);
            hotRectangle = checkBounds;
            return checkBounds.Contains(p);
        }

        private void NavBarControl_MouseClick(object sender, MouseEventArgs e)
        {
            if (!IsCheckBox(e.Location))
                return;
            Checked = !Checked;
            NavBarControl.Invalidate(hotRectangle);
            if (CheckedChanged != null)
                CheckedChanged(sender, e);
        }
    }
}

这篇关于如何在NavBarGroup中获取CheckEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆