개발/DevExpress

ListBoxControl 특정 조건에서 Font 변경하기

RainyJune 2023. 3. 8. 17:05

ListBoxControl 내 item에서 item 내용이 특정 조건이 만족하는 경우 Font를 Bold로 적용하고 싶어서 적용한 방법이다.

해당 처리는 DrawItem event를 통해서 처리할 수 있다.

private void listBoxControlName_DrawItem(object sender, ListBoxDrawItemEventArgs e)
{
	ListBoxControl listBox = sender as ListBoxControl;
	if (listBox == null || e.Index < 0) return;

	try
	{
		string itemText = listBox.GetItemText(e.Index);
		if (string.IsNullOrEmpty(itemText) == true) return;
    
		Color backColor = Color.FromArgb(255, 255, 255); // Default Color : White

		if ((e.State & DrawItemState.Selected) != 0)
		{
			backColor = Color.FromArgb(230, 240, 242);
		}
        
		e.Cache.FillRectangle(backColor, e.Bounds);
		ControlPaint.DrawBorder3D(e.Graphics, e.Bounds);
		
		FontStyle fontStyle = FontStyle.Regular;

		if (condition...)
		{
			fontStyle = FontStyle.Bold;   
		}
		e.Cache.DrawString(itemText, new Font(e.Appearance.Font.Name, e.Appearance.Font.Size, fontStyle), new SolidBrush(Color.Black), e.Bounds, e.Appearance.GetStringFormat());

		e.Handled = true;
	}
	catch (Exception ex)
	{
		throw Exception ...
	}
}