我已经用谷歌搜索了这个问题的答案,但我似乎找不到任何好的可靠例子。我创建了一个名为 StarControl 的自定义星级用户控件。该控件基本上是五个相互水平相邻的图片框,我有以下代码:
public partial class StarControl : UserControl
{
private enum StarTypes
{
Hollow,
Filled
}
private readonly StarTypes[] _stars;
private int _rating;
public StarControl()
{
InitializeComponent();
Locked = false;
_stars = new StarTypes[5];
_stars[0] = StarTypes.Hollow;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 0;
SetStars();
}
public bool Locked
{
get;
set;
}
public int Rating
{
get { return _rating; }
set { _rating = value; SetRating(); }
}
private void SetRating()
{
if (_rating == 0)
{
_stars[0] = StarTypes.Hollow;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
}
if (_rating == 1)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
}
if (_rating == 2)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
}
if (_rating == 3)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Filled;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
}
if (_rating == 4)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Filled;
_stars[3] = StarTypes.Filled;
_stars[4] = StarTypes.Hollow;
}
if (_rating == 5)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Filled;
_stars[3] = StarTypes.Filled;
_stars[4] = StarTypes.Filled;
}
SetStars();
}
private void SetStars()
{
pbStar1.Image = _stars[0] == StarTypes.Hollow
? Properties.Resources.star_hollow
: Properties.Resources.star_filled;
pbStar2.Image = _stars[1] == StarTypes.Hollow
? Properties.Resources.star_hollow
: Properties.Resources.star_filled;
pbStar3.Image = _stars[2] == StarTypes.Hollow
? Properties.Resources.star_hollow
: Properties.Resources.star_filled;
pbStar4.Image = _stars[3] == StarTypes.Hollow
? Properties.Resources.star_hollow
: Properties.Resources.star_filled;
pbStar5.Image = _stars[4] == StarTypes.Hollow
? Properties.Resources.star_hollow
: Properties.Resources.star_filled;
}
private void PbStar1MouseEnter(object sender, EventArgs e)
{
if (!Locked)
{
pbStar1.Image = Properties.Resources.star_filled;
pbStar2.Image = Properties.Resources.star_hollow;
pbStar3.Image = Properties.Resources.star_hollow;
pbStar4.Image = Properties.Resources.star_hollow;
pbStar5.Image = Properties.Resources.star_hollow;
}
}
private void PbStar1MouseLeave(object sender, EventArgs e)
{
if (!Locked)
{
SetStars();
}
}
private void PbStar2MouseEnter(object sender, EventArgs e)
{
if (!Locked)
{
pbStar1.Image = Properties.Resources.star_filled;
pbStar2.Image = Properties.Resources.star_filled;
pbStar3.Image = Properties.Resources.star_hollow;
pbStar4.Image = Properties.Resources.star_hollow;
pbStar5.Image = Properties.Resources.star_hollow;
}
}
private void PbStar2MouseLeave(object sender, EventArgs e)
{
if (!Locked)
{
SetStars();
}
}
private void PbStar3MouseEnter(object sender, EventArgs e)
{
if (!Locked)
{
pbStar1.Image = Properties.Resources.star_filled;
pbStar2.Image = Properties.Resources.star_filled;
pbStar3.Image = Properties.Resources.star_filled;
pbStar4.Image = Properties.Resources.star_hollow;
pbStar5.Image = Properties.Resources.star_hollow;
}
}
private void PbStar3MouseLeave(object sender, EventArgs e)
{
if (!Locked)
{
SetStars();
}
}
private void PbStar4MouseEnter(object sender, EventArgs e)
{
if (!Locked)
{
pbStar1.Image = Properties.Resources.star_filled;
pbStar2.Image = Properties.Resources.star_filled;
pbStar3.Image = Properties.Resources.star_filled;
pbStar4.Image = Properties.Resources.star_filled;
pbStar5.Image = Properties.Resources.star_hollow;
}
}
private void PbStar4MouseLeave(object sender, EventArgs e)
{
if (!Locked)
{
SetStars();
}
}
private void PbStar5MouseEnter(object sender, EventArgs e)
{
if (!Locked)
{
pbStar1.Image = Properties.Resources.star_filled;
pbStar2.Image = Properties.Resources.star_filled;
pbStar3.Image = Properties.Resources.star_filled;
pbStar4.Image = Properties.Resources.star_filled;
pbStar5.Image = Properties.Resources.star_filled;
}
}
private void PbStar5MouseLeave(object sender, EventArgs e)
{
if (!Locked)
{
SetStars();
}
}
private void PbStar1MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && !Locked)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 1;
}
if (e.Button == MouseButtons.Right && !Locked)
{
_stars[0] = StarTypes.Hollow;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 0;
}
SetStars();
}
private void PbStar2MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && !Locked)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 2;
}
if (e.Button == MouseButtons.Right && !Locked)
{
_stars[0] = StarTypes.Hollow;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 0;
}
SetStars();
}
private void PbStar3MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && !Locked)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Filled;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 3;
}
if (e.Button == MouseButtons.Right && !Locked)
{
_stars[0] = StarTypes.Hollow;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 0;
}
SetStars();
}
private void PbStar4MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && !Locked)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Filled;
_stars[3] = StarTypes.Filled;
_stars[4] = StarTypes.Hollow;
Rating = 4;
}
if (e.Button == MouseButtons.Right && !Locked)
{
_stars[0] = StarTypes.Hollow;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 0;
}
SetStars();
}
private void PbStar5MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && !Locked)
{
_stars[0] = StarTypes.Filled;
_stars[1] = StarTypes.Filled;
_stars[2] = StarTypes.Filled;
_stars[3] = StarTypes.Filled;
_stars[4] = StarTypes.Filled;
Rating = 5;
}
if (e.Button == MouseButtons.Right && !Locked)
{
_stars[0] = StarTypes.Hollow;
_stars[1] = StarTypes.Hollow;
_stars[2] = StarTypes.Hollow;
_stars[3] = StarTypes.Hollow;
_stars[4] = StarTypes.Hollow;
Rating = 0;
}
SetStars();
}
}
控制工作得很好。在我的表单上,我有一个 DataGridView 控件,我正在尝试使用集合中的行动态填充 DataGridView。该集合只是这个类的一个集合:
[Serializable]
public class Rating
{
public string VendorName { get; set; }
public int VendorRating { get; set; }
}
public List<Rating> _myRatings;
VendorName
只是一个字符串,VendorRating
是一个 int,表示 0-5 之间的数字。通过在 my 上设置我的Rating
属性StarControl
,它将显示该数量的星星。我想做的是弄清楚如何让我的StarControl
用户控件显示在DataGridView
. 有人可以为我提供一个如何做到这一点的例子吗?
我已经看过有关该主题的MSDN 文章,但我认为这不适用于我,因为它们只是继承自DataGridViewTextBoxCell
,我的控件比日期/时间文本框更复杂。