Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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
Winforms 如何在NavBarGroup中获得选中编辑_Winforms_Devexpress - Fatal编程技术网

Winforms 如何在NavBarGroup中获得选中编辑

Winforms 如何在NavBarGroup中获得选中编辑,winforms,devexpress,Winforms,Devexpress,我们需要在NavBarGroup的标题中放置一个复选框(及其标题)。有办法做到这一点吗?我们创建了一个NavBarGroupChecked类(NavBarGroupChecked.cs),该类继承自NavBarGroup,可以直接插入以替换它。它添加了一个RepositoryItemCheckEdit成员,该成员跟踪复选框并实现自定义绘制。它有一个Checked属性,告诉您是否已选中,以及一个在选中状态更改时将调用的事件。差不多就是这样。只是顺道来上班 代码如下所示 //从http://www.

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

我们创建了一个NavBarGroupChecked类(NavBarGroupChecked.cs),该类继承自NavBarGroup,可以直接插入以替换它。它添加了一个RepositoryItemCheckEdit成员,该成员跟踪复选框并实现自定义绘制。它有一个Checked属性,告诉您是否已选中,以及一个在选中状态更改时将调用的事件。差不多就是这样。只是顺道来上班

代码如下所示

//从http://www.devexpress.com/example=E2061
使用制度;
使用系统组件模型;
使用系统图;
运用系统反思;
使用System.Windows.Forms;
使用DevExpress.XtraEditors.Drawing;
使用DevExpress.XtraEditors.Repository;
使用DevExpress.XtraEditors.ViewInfo;
使用DevExpress.XtraNavBar;
使用DevExpress.XtraNavBar.ViewInfo;
命名空间autotagore.net.windward.controls
{
/// 
///标题中有复选框(带标题)的NavBarGroup。
/// 
公共类NavBarGroup已选中:NavBarGroup
{
/// 
///更改选中的属性值时发生。
/// 
公共事件事件处理程序CheckedChanged;
专用常量int复选框宽度=15;
私有布尔锁定;
private RepositoryItemCheckEdit\u GroupEdit;
专用导航栏控制_导航栏控制;
私有矩形;
/// 
///使用指定的标题初始化类的新实例。
/// 
///表示导航栏组标题的字符串。
公共导航组已选中(字符串标题)
:base(标题)
{
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\u ItemChanged;
}
私有void NavBarGroupChecked\u ItemChanged(对象发送者,System.EventArgs e)
{
如果(导航栏!=导航栏控制)
NavBarControl=NavBar;
} 
/// 
///创建类的实例。
/// 
公共NavBarGroupChecked()
{
ctor();
}
/// 
///拥有此的NavBarControl。必须将其设置为有效。
/// 
专用导航栏控件导航栏控件
{
获取{return\u NavBarControl;}
设置{UnsubscribeEvents(value);_NavBarControl=value;SubscribeEvents(value);}
}
私有无效订阅事件(NavBarControl NavBarControl)
{
如果(navBarControl==null)
返回;
NavBarControl.CustomDrawGroupCaption+=NavBarControl\u CustomDrawGroupCaption;
NavBarControl.MouseClick+=NavBarControl\u MouseClick;
}
私有无效取消订阅事件(NavBarControl NavBarControl)
{
如果(navBarControl!=null)
返回;
NavBarControl.CustomDrawGroupCaption-=NavBarControl\u CustomDrawGroupCaption;
NavBarControl.MouseClick-=NavBarControl\u MouseClick;
}
/// 
///如果选中该框,则为true。
/// 
公共布尔检查{get;set;}
/// 
///标题末尾复选框的缩进。
/// 
public int CheckIndent{get;set;}
///
///标题中显示的复选框。
///
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
公共存储项检查编辑组编辑
{
获取{return\u GroupEdit;}
设置{u GroupEdit=value;}
}
私有矩形GetCheckBoxBounds(矩形固定字幕边界)
{
返回新矩形(fixedCaptionBounds.Right-CHECK_BOX_WIDTH-CheckIndent,fixedCaptionBounds.Top,CHECK_BOX_WIDTH,fixedCaptionBounds.Height);
}
需要私人布尔值(NavBarGroup组)
{
返回GroupEdit!=null&&NavBarControl!=null&&isLocked&&group==this;
}
私有void NavBarControl_CustomDrawGroupCaption(对象发送方,CustomDrawNavBarElementEventArgs e)
{
NavGroupInfoArgs infoArgs=(NavGroupInfoArgs)e.ObjectInfo;
如果需要(!isCustomDraw(infoArgs.Group))
返回;
尝试
{
isLocked=true;
BaseNavGroupPainter painter=NavBarControl.View.CreateGroupPainter(NavBarControl);
矩形checkBoxBounds=GetCheckBoxBounds(infoArgs.CaptionBounds);
painter.DrawObject(infoArgs);
DrawCheckBox(如图形、复选框边界);
e、 已处理=正确;
}
最后
{
isLocked=false;
}
}
private void DrawCheckBox(图形g,矩形r)
{
BaseEditPainter=GroupEdit.CreatePainter();
BaseEditViewInfo=GroupEdit.CreateViewInfo();
info.EditValue=已选中;
SizeF textBounds=info.Appearance.CalcTextSize(g,GroupEdit.Caption,500);
int totalWidth=(int)textBounds.Width+r.Width+10;
info.Bounds=新矩形(右-总宽,右,总宽,右高);
信息CalcViewInfo(g);
ControlGraphicsInfoArgs args=新的ControlGraphicsInfoArgs(信息,new DevExpress.Utils.Drawing.GraphicsCache(g),r);
画家。画画(args);
args.Cache.Dispose();
}
私有静态NavBarViewInfo
// 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);
        }
    }
}
        private void Form1_Load(object sender, EventArgs e)
    {
        var item = navBarControl1.Groups.Add(new NavBarGroupChecked("NavBarGroupCheckbox", navBarControl1)) as NavBarGroupChecked;
        item.Hint = "my hint";
        item.CheckedChanged += checkchanged;
    }

    private void checkchanged(object sender, EventArgs e)
    {
        MessageBox.Show("It Changed");
    }
// 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 NavBarCheckTest
{
    /// <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, NavBarControl parent = null)
            : base(caption)
        {
            ctor(parent);
        }

        private void ctor(NavBarControl parent = null)
        {
            GroupEdit = new RepositoryItemCheckEdit { GlyphAlignment = DevExpress.Utils.HorzAlignment.Far };
            GroupEdit.Caption = string.Empty;
            GroupEdit.Appearance.Options.UseTextOptions = true;
            GroupEdit.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            GroupEdit.GlyphAlignment = DevExpress.Utils.HorzAlignment.Far;
            ItemChanged += NavBarGroupChecked_ItemChanged;
            if (parent != null)
                NavBarControl = parent;
        }

        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(NavBarControl parent = null)
        {
            ctor(parent);
        }

        /// <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 int GetCheckBoxWidth()
        {
            return CheckIndent * 2 + 10;
        }

        //private Rectangle GetCaptionBounds(Rectangle originalCaptionBounds)
        //{
        //    return new Rectangle(originalCaptionBounds.Location, new Size(originalCaptionBounds.Width - GetCheckBoxWidth(), originalCaptionBounds.Height));
        //}

        private Rectangle GetCheckBoxBounds(Rectangle fixedCaptionBounds)
        {
            return new Rectangle(fixedCaptionBounds.Right - CHECK_BOX_WIDTH - CheckIndent, fixedCaptionBounds.Top, CHECK_BOX_WIDTH, fixedCaptionBounds.Height); 
            //return new Rectangle(fixedCaptionBounds.Right, fixedCaptionBounds.Top, GetCheckBoxWidth(), 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 originalCaptionBounds = new Rectangle(infoArgs.CaptionClientBounds.X, infoArgs.CaptionClientBounds.Y, infoArgs.CaptionClientBounds.Width - infoArgs.ButtonBounds.Width, infoArgs.CaptionClientBounds.Height);
                Rectangle checkBoxBounds = GetCheckBoxBounds(originalCaptionBounds);

                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 originalCaptionBounds = new Rectangle(groupInfo.CaptionClientBounds.X, groupInfo.CaptionClientBounds.Y, groupInfo.CaptionClientBounds.Width - groupInfo.ButtonBounds.Width, groupInfo.CaptionClientBounds.Height);
            Rectangle checkBounds = GetCheckBoxBounds(originalCaptionBounds);

            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);
        }
    }
}