You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using POSV;
using POSV.Entity;
using POSV.Component;
using POSV.ThirdPartyOrderData;
namespace POSV.WeiXin
{
[ToolboxItem(true)]
public partial class WeixinCategoryX : BaseUserControl
{
/// <summary>
/// 商品分类控件元素默认宽度
/// </summary>
public const int CATEGORY_DEFAULT_WIDTH = 100;
private List<string> selectGoodsIdList = new List<string>();
/// <summary>
/// 选中的品类列表
/// </summary>
public Dictionary<string, ListCategory> SelectedCategorys { get; set; }
private List<ListCategory> _dataSource;
public WeixinCategoryX()
{
InitializeComponent();
this.itemPanel.ResizeItemsToFit = false;
this.itemPanel.BackgroundStyle.Class = "MetroTileGroupTitle";//很关键
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
//计算能显示的列数
this.ItemWidth = CATEGORY_DEFAULT_WIDTH;
if (this.ItemWidth + this.ItemSpace != 0)
{
this.Columns = (this.itemPanel.Width / (this.ItemWidth + this.ItemSpace));
}
if (this.Columns != 0)
{
this.ItemWidth = Convert.ToInt16((this.itemPanel.Width * 1.0 / (this.Columns * 1.0)) - this.ItemSpace);
BindDataSource(this._dataSource);
}
}
public event WeixinCategoryXEventHandler CategoryXCheckedChanged;
protected virtual void OnCategoryXCheckedChanged(WeixinCategoryXEventArgs e)
{
CategoryXCheckedChanged?.Invoke(this, e);
}
public void BindDataSource(List<ListCategory> dataSource)
{
try
{
if(this.SelectedCategorys == null)
{
this.SelectedCategorys = new Dictionary<string, ListCategory>();
}
this._dataSource = dataSource;
if (this._dataSource == null)
{
this._dataSource = new List<ListCategory>();
}
this.TotalCount = this._dataSource.Count;
this.PageSize = this.Columns * this.Rows;
//清空历史数据
this.itemPanel.Items.Clear();
var pager = ListPager(this._dataSource);
ItemContainer mainContainer = new ItemContainer();
mainContainer.Name = "categoryContainer";
mainContainer.MultiLine = (this.Rows > 1);
mainContainer.ItemSpacing = this.ItemSpace;
mainContainer.LayoutOrientation = eOrientation.Horizontal;
mainContainer.BackgroundStyle.Padding = 0;
mainContainer.BackgroundStyle.PaddingLeft = 0;
mainContainer.BackgroundStyle.PaddingRight = 0;
foreach (ListCategory entity in pager)
{
MetroTileItem item = new MetroTileItem();
item.Name = entity.id;
item.TileStyle.PaddingLeft = item.TileStyle.PaddingRight = -2;
item.EnableMarkup = true;
item.OptionGroup = "category";
item.TileStyle.BorderWidth = 1;
item.TileStyle.CornerDiameter = 18;
item.TileStyle.CornerType = eCornerType.Rounded;
//正常情况下的背景颜色
item.TileStyle.BackColor = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.BackColor2 = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.BorderColor = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.TextColor = ColorTranslator.FromHtml("#444444");
if (this.SelectedCategorys.ContainsKey(entity.id))
{
//选中情况下的背景颜色
item.TileStyle.BackColor = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.BackColor2 = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.BorderColor = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.TextColor = ColorTranslator.FromHtml("#FFFFFF");
}
item.TitleTextAlignment = ContentAlignment.MiddleCenter;
item.Checked = false;
item.CheckBehavior = eMetroTileCheckBehavior.None;
//显示大小
item.TileSize = new Size(this.ItemWidth, this.ItemHeight);
item.TileStyle.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
item.TileStyle.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
item.TileStyle.TextColor = ColorTranslator.FromHtml("#444444");
//绑定品类名称到Text属性
item.Text = entity.name;
//绑定品类ID到Tag属性
item.Tag = entity;
item.MouseDown -= OnCategoryMouseDown;
item.MouseDown += OnCategoryMouseDown;
mainContainer.SubItems.Add(item);
}
this.itemPanel.Items.Insert(0, mainContainer);
this.itemPanel.Invalidate();
}
catch(Exception ex)
{
LOGGER.Error(ex, "构建小程序商品分类控件内容异常");
}
}
private void OnCategoryMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MetroTileItem item = (MetroTileItem)sender;
var category = item.Tag as ListCategory;
//已经被选中,再次点击取消选中
if (this.SelectedCategorys.ContainsKey(category.id))
{
//正常情况下的背景颜色
item.TileStyle.BackColor = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.BackColor2 = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.BorderColor = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml("#E7EAF1");
item.TileStyle.TextColor = ColorTranslator.FromHtml("#444444");
this.SelectedCategorys.Remove(category.id);
}
else
{
//选中情况下的背景颜色
item.TileStyle.BackColor = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.BackColor2 = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.BorderColor = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml("#00C7BA");
item.TileStyle.TextColor = ColorTranslator.FromHtml("#FFFFFF");
this.SelectedCategorys.Add(category.id, category);
}
OnCategoryXCheckedChanged(new WeixinCategoryXEventArgs(this.SelectedCategorys));
}
}
/// <summary>
/// 分页List数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public List<T> ListPager<T>(List<T> data)
{
var result = new List<T>();
result.AddRange(data.Skip((this.PageNumber - 1) * this.PageSize).Take(this.PageSize));
return result;
}
private MetroTileItem _selectedItem = null;
public MetroTileItem SelectedItem
{
get { return this._selectedItem; }
private set { this._selectedItem = value; }
}
/// <summary>
/// 每页数量
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 总页数
/// </summary>
public int PageCount => this.PageSize <= 0 ? 0 : ((TotalCount + this.PageSize - 1) / this.PageSize);
/// <summary>
/// 当前页码
/// </summary>
public int PageNumber { get; set; } = 1;
/// <summary>
/// 总数量
/// </summary>
public int TotalCount { get; private set; }
private int _columns = 1;
/// <summary>
/// 显示的列数
/// </summary>
public int Columns
{
get
{
return _columns;
}
set
{
_columns = (value < 0 ? 1 : value);
}
}
private int _rows = 1;
/// <summary>
/// 显示行数
/// </summary>
public int Rows
{
get
{
return _rows;
}
set
{
_rows = (value <= 0 ? 1 : value);
}
}
private int _itemHeight = 38;
public int ItemHeight
{
get
{
return _itemHeight;
}
set
{
_itemHeight = value;
}
}
private int _itemWidth = CATEGORY_DEFAULT_WIDTH;
public int ItemWidth
{
get
{
return _itemWidth;
}
set
{
this._itemWidth = value;
}
}
private int _itemSpace = 2;
/// <summary>
/// 控件间距
/// </summary>
public int ItemSpace
{
get
{
return _itemSpace;
}
set
{
this._itemSpace = value;
}
}
private void OnPrePageClick(object sender, EventArgs e)
{
if (this.HasPreviousPage)
{
this.PageNumber--;
BindDataSource(this._dataSource);
}
}
private void OnNextPageClick(object sender, EventArgs e)
{
if (this.HasNextPage)
{
this.PageNumber++;
BindDataSource(this._dataSource);
}
}
//是否有上一页
public bool HasPreviousPage
{
get { return PageNumber > 1; }
}
//是否有下一页
public bool HasNextPage
{
get { return PageNumber < this.PageCount; }
}
}
public delegate void WeixinCategoryXEventHandler(object sender, WeixinCategoryXEventArgs e);
public class WeixinCategoryXEventArgs : EventArgs
{
public Dictionary<string,ListCategory> Data;
public WeixinCategoryXEventArgs(Dictionary<string, ListCategory> data)
{
this.Data = data;
}
}
}