📄 datagridviewradiobuttoncell.cs
字号:
{
base.DisplayMember = value;
InitializeDisplayMemberPropertyDescriptor(value);
}
}
/// <summary>
/// Overrides the base implementation to replace the 'complex editing experience'
/// with a 'simple editing experience'.
/// </summary>
public override Type EditType
{
get
{
// Return null since no editing control is used for the editing experience.
return null;
}
}
/// <summary>
/// Custom property that represents the maximum number of radio buttons shown by the cell.
/// </summary>
[
DefaultValue(DATAGRIDVIEWRADIOBUTTONCELL_defaultMaxDisplayedItems)
]
public int MaxDisplayedItems
{
get
{
return this.maxDisplayedItems;
}
set
{
if (value < 1 || value > 100)
{
throw new ArgumentOutOfRangeException("MaxDisplayedItems");
}
this.maxDisplayedItems = value;
if (this.DataGridView != null && !this.DataGridView.IsDisposed && !this.DataGridView.Disposing)
{
if (this.RowIndex == -1)
{
// Invalidate and autosize column
this.DataGridView.InvalidateColumn(this.ColumnIndex);
// TODO: Add code to autosize the cell's column, the rows, the column headers
// and the row headers depending on their autosize settings.
// The DataGridView control does not expose a public method that takes care of this.
}
else
{
// The DataGridView control exposes a public method called UpdateCellValue
// that invalidates the cell so that it gets repainted and also triggers all
// the necessary autosizing: the cell's column and/or row, the column headers
// and the row headers are autosized depending on their autosize settings.
this.DataGridView.UpdateCellValue(this.ColumnIndex, this.RowIndex);
}
}
}
}
/// <summary>
/// Called internally by the DataGridViewRadioButtonColumn class to avoid the invalidation
/// done by the MaxDisplayedItems setter above (for performance reasons).
/// </summary>
internal int MaxDisplayedItemsInternal
{
set
{
Debug.Assert(value >= 1 && value <= 100);
this.maxDisplayedItems = value;
}
}
/// <summary>
/// Utility function that returns the standard thickness (in pixels) of the four borders of the cell.
/// </summary>
private Rectangle StandardBorderWidths
{
get
{
if (this.DataGridView != null)
{
DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder = new DataGridViewAdvancedBorderStyle(), dgvabsEffective;
dgvabsEffective = AdjustCellBorderStyle(this.DataGridView.AdvancedCellBorderStyle,
dataGridViewAdvancedBorderStylePlaceholder,
false /*singleVerticalBorderAdded*/,
false /*singleHorizontalBorderAdded*/,
false /*isFirstDisplayedColumn*/,
false /*isFirstDisplayedRow*/);
return BorderWidths(dgvabsEffective);
}
else
{
return Rectangle.Empty;
}
}
}
/// <summary>
/// Overrides the DataGridViewComboBox's implementation of the ValueMember property to
/// update the valueMemberProperty member.
/// </summary>
public override string ValueMember
{
get
{
return base.ValueMember;
}
set
{
base.ValueMember = value;
InitializeValueMemberPropertyDescriptor(value);
}
}
/// <summary>
/// Utility function that returns the cell state inherited from the owning row and column.
/// </summary>
private DataGridViewElementStates CellStateFromColumnRowStates(DataGridViewElementStates rowState)
{
Debug.Assert(this.DataGridView != null);
Debug.Assert(this.ColumnIndex >= 0);
DataGridViewElementStates orFlags = DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Resizable | DataGridViewElementStates.Selected;
DataGridViewElementStates andFlags = DataGridViewElementStates.Displayed | DataGridViewElementStates.Frozen | DataGridViewElementStates.Visible;
DataGridViewElementStates cellState = (this.OwningColumn.State & orFlags);
cellState |= (rowState & orFlags);
cellState |= ((this.OwningColumn.State & andFlags) & (rowState & andFlags));
return cellState;
}
/// <summary>
/// Custom implementation of the Clone method to copy over the special properties of the cell.
/// </summary>
public override object Clone()
{
DataGridViewRadioButtonCell dataGridViewCell = base.Clone() as DataGridViewRadioButtonCell;
if (dataGridViewCell != null)
{
dataGridViewCell.MaxDisplayedItems = this.MaxDisplayedItems;
}
return dataGridViewCell;
}
/// <summary>
/// Computes the layout of the cell and optionally paints it.
/// </summary>
private void ComputeLayout(Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates cellState,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts,
bool paint)
{
if (paint && DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.Border))
{
// Paint the borders first
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
}
// Discard the space taken up by the borders.
Rectangle borderWidths = BorderWidths(advancedBorderStyle);
Rectangle valBounds = cellBounds;
valBounds.Offset(borderWidths.X, borderWidths.Y);
valBounds.Width -= borderWidths.Right;
valBounds.Height -= borderWidths.Bottom;
SolidBrush backgroundBrush = null;
try
{
Point ptCurrentCell = this.DataGridView.CurrentCellAddress;
bool cellCurrent = ptCurrentCell.X == this.ColumnIndex && ptCurrentCell.Y == rowIndex;
bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0;
bool mouseOverCell = cellBounds.Contains(this.DataGridView.PointToClient(Control.MousePosition));
if (DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.SelectionBackground) && cellSelected)
{
backgroundBrush = new SolidBrush(cellStyle.SelectionBackColor);
}
else
{
backgroundBrush = new SolidBrush(cellStyle.BackColor);
}
if (paint && DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.Background) && backgroundBrush.Color.A == 255)
{
Rectangle backgroundRect = valBounds;
backgroundRect.Intersect(clipBounds);
graphics.FillRectangle(backgroundBrush, backgroundRect);
}
// Discard the space taken up by the padding area.
if (cellStyle.Padding != Padding.Empty)
{
valBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top);
valBounds.Width -= cellStyle.Padding.Horizontal;
valBounds.Height -= cellStyle.Padding.Vertical;
}
Rectangle errorBounds = valBounds;
Rectangle scrollBounds = valBounds;
this.layout.ScrollingNeeded = GetScrollingNeeded(graphics, rowIndex, cellStyle, valBounds.Size);
if (this.layout.ScrollingNeeded)
{
this.layout.ScrollButtonsSize = ScrollBarRenderer.GetSizeBoxSize(graphics, ScrollBarState.Normal);
// Discard the space required for displaying the 2 scroll buttons
valBounds.Width -= this.layout.ScrollButtonsSize.Width;
}
valBounds.Inflate(-DATAGRIDVIEWRADIOBUTTONCELL_margin, -DATAGRIDVIEWRADIOBUTTONCELL_margin);
// Layout / paint the radio buttons themselves
this.layout.RadioButtonsSize = RadioButtonRenderer.GetGlyphSize(graphics, RadioButtonState.CheckedNormal);
this.layout.DisplayedItemsCount = 0;
this.layout.TotallyDisplayedItemsCount = 0;
if (valBounds.Width > 0 && valBounds.Height > 0)
{
this.layout.FirstDisplayedItemLocation = new Point(valBounds.Left + DATAGRIDVIEWRADIOBUTTONCELL_margin, valBounds.Top + DATAGRIDVIEWRADIOBUTTONCELL_margin);
int textHeight = cellStyle.Font.Height;
int itemIndex = this.layout.FirstDisplayedItemIndex;
Rectangle radiosBounds = valBounds;
while (itemIndex < this.Items.Count &&
itemIndex < this.layout.FirstDisplayedItemIndex + this.maxDisplayedItems &&
radiosBounds.Height > 0)
{
if (paint && DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.ContentBackground))
{
Rectangle itemRect = radiosBounds;
itemRect.Intersect(clipBounds);
if (!itemRect.IsEmpty)
{
bool itemReadOnly = (cellState & DataGridViewElementStates.ReadOnly) != 0;
bool itemSelected = false;
if (formattedValue != null)
{
object displayValue = GetItemDisplayValue(this.Items[itemIndex]);
if (formattedValue.Equals(displayValue))
{
itemSelected = true;
}
}
PaintItem(graphics,
radiosBounds,
rowIndex,
itemIndex,
cellStyle,
itemReadOnly,
itemSelected,
mouseOverCell,
cellCurrent && this.focusedItemIndex == itemIndex && DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.Focus));
}
}
itemIndex++;
radiosBounds.Y += textHeight + DATAGRIDVIEWRADIOBUTTONCELL_margin;
radiosBounds.Height -= (textHeight + DATAGRIDVIEWRADIOBUTTONCELL_margin);
if (radiosBounds.Height >= 0)
{
this.layout.TotallyDisplayedItemsCount++;
}
this.layout.DisplayedItemsCount++;
}
this.layout.ContentBounds = new Rectangle(this.layout.FirstDisplayedItemLocation, new Size(this.layout.RadioButtonsSize.Width, this.layout.DisplayedItemsCount * (textHeight + DATAGRIDVIEWRADIOBUTTONCELL_margin)));
}
else
{
this.layout.FirstDisplayedItemLocation = new Point(-1, -1);
this.layout.ContentBounds = Rectangle.Empty;
}
if (this.layout.ScrollingNeeded)
{
// Layout / paint the 2 scroll buttons
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -