DevExpress控件使用之SearchLookUpEdit (多选)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014682870/article/details/73505786

效果图(可多选,弹出窗体添加控件)

弹出窗体界面
选中数据后展示

SearchLookUpEdit(多选)

控件的属性和事件使用

1.设置多选属性

这里写图片描述

//---------------需要的一些属性和方法------------------
//楼盘内容类
public class LookUpMultSelectValues
{
    public string FindText { get; set; }
    public List<string> SelectedValues { get; set; }
    public List<string> SelectedDisplays { get; set; }
}
//楼盘内容存储变量
private List<LookUpMultSelectValues> luValues = new List<LookUpMultSelectValues>();
//选择楼盘时,动态显示选中个数
public LabelControl searchResult = new LabelControl() { Text = "已选择 0 个楼盘" };
//楼盘选中的数据去重
private List<string>[] GetLuValues()
 {
     List<string> r = new List<string>();
     foreach (var a in luValues)
     {
         r.AddRange(a.SelectedValues);
     }

     List<string> b = new List<string>();
     foreach (var a in luValues)
     {
         b.AddRange(a.SelectedDisplays);
     }
     return new[] { r.Distinct().ToList<string>(), b.Distinct().ToList<string>() };
 }

2.弹出窗体事件

这里写图片描述

//弹出窗体,并动态添加控件(确认按钮,取消按钮,选中结果显示标签)
//窗体弹出事件
private void sleEstate_Popup(object sender, EventArgs e)
{
    //得到当前SearchLookUpEdit弹出窗体
    PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
    SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
    LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
    //如果窗体内空间没有确认按钮,则自定义确认simplebutton,取消simplebutton,选中结果label
    if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
    {
        //得到空的空间
        EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

        //得到取消按钮,重写点击事件
        Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
        LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
        clearBtn.Click += clearBtn_Click;

        //添加一个simplebutton控件(确认按钮)
        LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
        myLCI.TextVisible = false;
        SimpleButton btOK = new SimpleButton() { Name = "btOK", Text = "确定" };
        btOK.Click += btOK_Click;
        myLCI.Control = btOK;
        myLCI.SizeConstraintsType = SizeConstraintsType.Custom;//控件的大小设置为自定义
        myLCI.MaxSize = clearLCI.MaxSize;
        myLCI.MinSize = clearLCI.MinSize;
        myLCI.Move(clearLCI, DevExpress.XtraLayout.Utils.InsertType.Left);

        //添加一个label控件(选中结果显示)
        LayoutControlItem msgLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(a.Parent);
        msgLCI.TextVisible = false;
        msgLCI.Control = searchResult;
        msgLCI.Move(a, DevExpress.XtraLayout.Utils.InsertType.Left);
        msgLCI.BestFitWeight = 100;
    }
}
//楼盘清除按钮事件
private void clearBtn_Click(object sender, EventArgs e)
{
    luValues.Clear();//将保存的数据清空
    sleEstate.EditValue = null;
    searchResult.Text = "已选择 0 个楼盘";
}
//楼盘确定按钮事件
private void btOK_Click(object sender, EventArgs e)
{
    sleEstate.ClosePopup();
}

3.按钮事件,关闭事件,自定义显示内容事件,值改变事件

这里写图片描述

//按钮点击
private void sleEstate_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
    //清除
    if (e.Button.Kind == ButtonPredefines.Clear)
    {
        luValues.Clear();
        sleEstate.EditValue = null;
        searchResult.Text = "已选择 0 个楼盘";
    }
}

//关闭
private void sleEstate_Closed(object sender, ClosedEventArgs e)
{
    var re = GetLuValues();
    sleEstate.EditValue = string.Join(",", re[0].ToArray());
    sleEstate.ToolTip = string.Join(",", re[1].ToArray());

}

//自定义显示内容
private void sleEstate_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
{
    if (FunctionHelper.GetValue(e.Value).Contains(","))
    {
        e.DisplayText = "多选";
    }
    else
    {
        var re = GetLuValues();
        if (re[0].Count == 1)
        {
            e.DisplayText = re[1].First();
        }
        else
        {
            e.DisplayText = "";
        }
    }
}
//值改变
private void sleEstate_EditValueChanged(object sender, EventArgs e)
{
    string estateId = FunctionHelper.GetValue(sleEstate.EditValue);
    if (estateId != "")
    {
        sleEstate.Properties.Buttons[1].Visible = true;
    }
    else
    {
        sleEstate.ToolTip = null;
        sleEstate.Properties.View.ClearSelection();
        sleEstate.Properties.Buttons[1].Visible = false;
    }
}
展开阅读全文

没有更多推荐了,返回首页