Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何自定义System.Windows.Forms.ListViewItem?_C#_.net_Winforms_Listview_Listviewitem - Fatal编程技术网

C# 如何自定义System.Windows.Forms.ListViewItem?

C# 如何自定义System.Windows.Forms.ListViewItem?,c#,.net,winforms,listview,listviewitem,C#,.net,Winforms,Listview,Listviewitem,我正在开发一个Office插件,并希望制作如下内容: 现在我正在使用ListView,但是ListViewItem,即使在平铺模式下,也不能自定义为我想要的。它最多只允许2行文本 任何人都可以帮助指向我可以使用的正确Windows窗体控件吗?我应该扩展ListView还是ListViewItem?有什么现有的解决方案吗 谢谢,我建议改用列表框。我将为你复制一个例子 在表单中添加一个列表框。然后,您可以在表单的OnLoad事件上编写与此类似的代码: private void Form1_

我正在开发一个Office插件,并希望制作如下内容:

现在我正在使用ListView,但是ListViewItem,即使在平铺模式下,也不能自定义为我想要的。它最多只允许2行文本

任何人都可以帮助指向我可以使用的正确Windows窗体控件吗?我应该扩展ListView还是ListViewItem?有什么现有的解决方案吗


谢谢,

我建议改用列表框。我将为你复制一个例子

在表单中添加一个列表框。然后,您可以在表单的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;
    }
private void Form1\u加载(对象发送方,事件参数e)
{
//这将更改列表框的行为,以便您可以自定义列表中每个项目的图形。
//固定模式使列表中的每一项都具有固定的大小
//如果大小不同,则可以使用DrawMode.OwnerDrawVariable。
listBox1.DrawMode=DrawMode.OwnerDrawFixed;
//这里我们定义列表中每个项目的高度。
listBox1.ItemHeight=40;
//在这里,我将只制作一个示例数据源,以模拟您试图复制的控件。
var数据集=新列表();
添加(新元组(“下午5:30-6:00”,“11个可用房间”);
添加(新元组(“下午6:00-6:30”,“12个可用房间”);
listBox1.DataSource=数据集;
}
现在,编辑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);
    }
private void listBox1\u DrawItem(对象发送方,DrawItemEventArgs e)
{
//此变量将保存底部文本的颜色-表示
//示例中的可用房间。
灌木丛;
//在这里,我们覆盖DrawItemEventArgs以更改选定对象的颜色
//项目背景为我们的首选之一。
//我改为SystemColor.Control,更像您试图复制的列表。
//此外,正如我在您的示例中看到的,选中房间文本部分的字体为黑色,而灰色
//如果没有选择颜色,我们也将通过设置正确的颜色来复制它
//在上面定义的变量上。
if((e.State&DrawItemState.Selected)=DrawItemState.Selected)
{
e=新的DrawItemEventArgs(如图形、字体、边界、,
e、 索引,例如状态^DrawItemState.Selected,例如ForeColor,SystemColor.Control);
roomsBrush=画笔。黑色;
}
其他的
{
roomsBrush=画笔。灰色;
}
//再看看你的例子,我注意到每个项目的底部都有一条灰色的线。
//让我们也复制一下。
var linePen=新笔(systembrusks.Control);
var lineStartPoint=新点(e.Bounds.Left、e.Bounds.Height+e.Bounds.Top);
var lineEndPoint=新点(e.Bounds.Width、e.Bounds.Height+e.Bounds.Top);
e、 绘图线(线笔、线起点、线端点);
//命令事件绘制项目的适当背景。
e、 牵引杆接地();
//在这里,您可以获得与当前正在绘制的项目关联的数据项。
var dataItem=listBox1.Items[e.Index]作为元组;
//在这里,我们将格式化与列表项的时间文本相对应的部分字体。
//你可以随意改变,我把它定义为粗体。
var-timeFont=新字体(“Microsoft无衬线字体”,8.25f,FontStyle.Bold);
//在这里,您可以使用定义的格式在列表项顶部绘制时间文本。
e、 Graphics.DrawString(dataItem.Item1,timeFont,brusks.Black,e.Bounds.Left+3,e.Bounds.Top+5);
//现在我们绘制Available rooms部分。首先我们定义字体。
var roomsFont=新字体(“Microsoft无衬线字体”,8.25f,FontStyle.Regular);
//最后,我们画这段文字。
e、 Graphics.DrawString(dataItem.Item2,roomsFont,roomsBrush,e.Bounds.Left+3,e.Bounds.Top+18);
}
而且,当我们跑步时,我们会有类似的感觉。这和你的例子很相似,不是吗


如果要进行更多更改,只需在DrawItem事件中处理图形。希望有帮助

我建议改用列表框。我将为你复制一个例子

在表单中添加一个列表框。然后,您可以在表单的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;
    }
private void Form1\u加载(对象发送方,事件参数e)
{
//这将更改列表框的行为,以便您可以自定义列表中每个项目的图形。
//固定模式使列表中的每一项都具有固定的大小
//如果大小不同,则可以使用DrawMode.OwnerDrawVariable。
listBox1.DrawMode=DrawMode.OwnerDrawFixed;
//这里我们定义列表中每个项目的高度。
listBox1.ItemHeight=40;
//在这里,我将只制作一个示例数据源,以模拟您试图复制的控件。
var数据集=新列表();
添加(新元组(“下午5:30-6:00”,“11个可用房间”);
添加(新元组(“下午6:00-6:30”,“12个可用房间”);
listBox1.DataSource=数据集;
}
现在,编辑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);
    }
private void listBox1\u DrawItem(对象发送方,DrawItemEventArgs e)
{
//此变量将保存底部文本的颜色-表示
//示例中的可用房间。
灌木丛;
//在这里,我们覆盖DrawItemEventArgs以更改选定对象的颜色
//项目背景为我们的首选之一。
//我改为SystemColor.Control,更像列表哟