2015-07-30 4 views
1

Я разрабатываю плагин Office, и хотел бы, чтобы произвести что-то вроде этого: screenshotКак настроить System.Windows.Forms.ListViewItem?

Сейчас я использую ListView, но ListViewItem, даже в режиме плитки, не настраивается на то, что я хочу. Он допускает до 2 строк текста max.

Любой может помочь указать на правильный элемент управления Windows Forms, который я могу использовать? Должен ли я расширять ListView или ListViewItem? Любое существующее решение?

Спасибо,

+0

Разве вы не можете просто наследовать этот класс? – Sweeper

ответ

3

Я рекомендую использовать ListBox вместо. Я воспроизведу пример для вас.

Поместите ListBox в вашу форму. После этого вы можете написать код, подобный этому на OnLoad событий формы:

private void Form1_Load(object sender, EventArgs e) 
    { 
     // This will change the ListBox behaviour, so you can customize the drawing of each item on the list. 
     // The fixed mode makes every item on the list to have a fixed size. If you want each item having 
     // a different size, you can use DrawMode.OwnerDrawVariable. 
     listBox1.DrawMode = DrawMode.OwnerDrawFixed; 

     // Here we define the height of each item on your list. 
     listBox1.ItemHeight = 40; 

     // Here i will just make an example data source, to emulate the control you are trying to reproduce. 
     var dataSet = new List<Tuple<string, string>>(); 

     dataSet.Add(new Tuple<string, string>("5:30 PM - 6:00 PM", "11 avaliable rooms")); 
     dataSet.Add(new Tuple<string, string>("6:00 PM - 6:30 PM", "12 available rooms")); 

     listBox1.DataSource = dataSet; 
    } 

Теперь редактировать событие ListBox DrawItem, и написать код, подобный этому:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     // This variable will hold the color of the bottom text - the one saying the count of 
     // the avaliable rooms in your example. 
     Brush roomsBrush; 

     // Here we override the DrawItemEventArgs to change the color of the selected 
     // item background to one of our preference. 
     // I changed to SystemColors.Control, to be more like the list you are trying to reproduce. 
     // Also, as I see in your example, the font of the room text part is black colored when selected, and gray 
     // colored when not selected. So, we are going to reproduce it as well, by setting the correct color 
     // on our variable defined above. 
     if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
     { 
      e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, 
       e.Index, e.State^DrawItemState.Selected, e.ForeColor, SystemColors.Control); 

      roomsBrush = Brushes.Black; 
     } 
     else 
     { 
      roomsBrush = Brushes.Gray; 
     } 

     // Looking more at your example, i noticed a gray line at the bottom of each item. 
     // Lets reproduce that, too. 
     var linePen = new Pen(SystemBrushes.Control); 
     var lineStartPoint = new Point(e.Bounds.Left, e.Bounds.Height + e.Bounds.Top); 
     var lineEndPoint = new Point(e.Bounds.Width, e.Bounds.Height + e.Bounds.Top); 

     e.Graphics.DrawLine(linePen, lineStartPoint, lineEndPoint); 

     // Command the event to draw the appropriate background of the item. 
     e.DrawBackground(); 

     // Here you get the data item associated with the current item being drawed. 
     var dataItem = listBox1.Items[e.Index] as Tuple<string, string>; 

     // Here we will format the font of the part corresponding to the Time text of your list item. 
     // You can change to wathever you want - i defined it as a bold font. 
     var timeFont = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold); 

     // Here you draw the time text on the top of the list item, using the format you defined. 
     e.Graphics.DrawString(dataItem.Item1, timeFont, Brushes.Black, e.Bounds.Left + 3, e.Bounds.Top + 5); 

     // Now we draw the avaliable rooms part. First we define our font. 
     var roomsFont = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular); 

     // And, finally, we draw that text. 
     e.Graphics.DrawString(dataItem.Item2, roomsFont, roomsBrush, e.Bounds.Left + 3, e.Bounds.Top + 18); 
    } 

И, когда мы имеем что-то подобное. Это очень похоже на ваш пример, не так ли?

Our example reproduced

Если вы хотите сделать больше изменений, вам просто нужно играть с чертежами, на мероприятии DrawItem. Надеюсь, поможет!

+0

Спасибо Ismael, я застрял в какой-то другой работе и вернусь к вам, когда смогу – Motoko