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.

336 lines
10 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;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class CategoryX : BaseUserControl
{
/// <summary>
/// 商品分类控件元素默认宽度
/// </summary>
public const int CATEGORY_DEFAULT_WIDTH = 100;
private List<ProductType> _dataSource;
public CategoryX()
{
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 CategoryXEventHandler CategoryXCheckedChanged;
protected virtual void OnCategoryXCheckedChanged(CategoryXEventArgs e)
{
CategoryXCheckedChanged?.Invoke(this, e);
}
public void BindDataSource(List<ProductType> dataSource)
{
try
{
this._dataSource = dataSource;
if (dataSource == null)
{
this._dataSource = new List<ProductType>();
}
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 (ProductType entity in pager)
{
MetroTileItem item = new MetroTileItem();
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");// Color.White;
item.TileStyle.BackColor2 = ColorTranslator.FromHtml("#E7EAF1");//Color.White;
item.TileStyle.BorderColor = ColorTranslator.FromHtml("#E7EAF1");//Color.White;
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml("#E7EAF1");//Color.White;
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)
{
var metro = sender as MetroTileItem;
metro.Checked = false;
if (metro.Tag == null) return;
foreach (BaseItem item in metro.Parent.SubItems)
{
if (item == metro) continue;
var b = item as MetroTileItem;
if (b != null && b.OptionGroup == metro.OptionGroup)
{
//正常情况下的背景颜色
b.TileStyle.BackColor = ColorTranslator.FromHtml("#E7EAF1");//Color.White;
b.TileStyle.BackColor2 = ColorTranslator.FromHtml("#E7EAF1");//Color.White;
b.TileStyle.BorderColor = ColorTranslator.FromHtml("#E7EAF1");//Color.White;
b.TileStyle.BorderColor2 = ColorTranslator.FromHtml("#E7EAF1");//Color.White;
b.TileStyle.TextColor = ColorTranslator.FromHtml("#444444");
}
}
//选中情况下的背景颜色
metro.TileStyle.BackColor = ColorTranslator.FromHtml("#00C7BA");
metro.TileStyle.BackColor2 = ColorTranslator.FromHtml("#00C7BA");
metro.TileStyle.BorderColor = ColorTranslator.FromHtml("#00C7BA");
metro.TileStyle.BorderColor2 = ColorTranslator.FromHtml("#00C7BA");
metro.TileStyle.TextColor = ColorTranslator.FromHtml("#FFFFFF");
OnCategoryXCheckedChanged(new CategoryXEventArgs((ProductType)metro.Tag));
}
/// <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 CategoryXEventHandler(object sender, CategoryXEventArgs e);
public class CategoryXEventArgs : EventArgs
{
public ProductType Data;
public CategoryXEventArgs(ProductType data)
{
this.Data = data;
}
}
}