【问题标题】:C# - .net controls as ComboBox ItemsC# - .net 控件作为组合框项
【发布时间】:2009-07-03 17:12:43
【问题描述】:

我们可以将 C# 文本框控件放置在 C# 下拉组合框中吗?

也就是说,当组合被下拉时,它的每个项目都会显示一个文本框。

【问题讨论】:

  • 您应该更具体一些...您在说什么技术? WPF、Windows 窗体、ASP.NET ?

标签: c# combobox


【解决方案1】:

在 windows 窗体中,这并不完全可能,但您可以截获导致组合框下拉的窗口消息,并改为显示面板或窗体。

作为一个起点:

public class UserControlComboBox : ComboBox, IMessageFilter
{
    public readonly MyControlClass UserControl = new MyControlClass();

    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == 0x0201) || (m.Msg == 0x0203))
        {
            if (DroppedDown)
                HideUserControl();
            else
                ShowUserControl();
       }
        else
        {
            base.WndProc(ref m);
        }
    }

    public bool PreFilterMessage(ref Message m)
    {
        // intercept mouse events
        if ((m.Msg >= 0x0200) && (m.Msg <= 0x020A))
        {
            if (this.UserControl.RectangleToScreen(this.UserControl.DisplayRectangle).Contains(Cursor.Position))
            {
                // clicks inside the user control, handle normally
                return false;
            }
            else
            {
                // clicks outside the user controlcollapse it.
                if ((m.Msg == 0x0201) || (m.Msg == 0x0203))
                    this.HideUserControl();
                return true;
            }
        }
        else return false;
    }

    public new bool DroppedDown 
    {
        get { return this.UserControl.Visible; } 
    }

    protected void ShowUserControl()
    {
        if (!this.Visible)
            return;

        this.UserControl.Anchor = this.Anchor;
        this.UserControl.BackColor = this.BackColor;
        this.UserControl.Font = this.Font;
        this.UserControl.ForeColor = this.ForeColor;

        // you can be cleverer than this if you need to
        this.UserControl.Top = this.Bottom;
        this.UserControl.Left = this.Left;
        this.UserControl.Width = Math.Max(this.UserControl.Width, this.Width);

        this.Parent.Controls.Add(this.UserControl);
        this.UserControl.Visible = true;
        this.UserControl.BringToFront();

        base.OnDropDown(EventArgs.Empty);

        // start listening for clicks
        Application.AddMessageFilter(this);
    }

    protected void HideUserControl()
    {
        Application.RemoveMessageFilter(this);

        base.OnDropDownClosed(EventArgs.Empty);
        this.UserControl.Visible = false;
        this.Parent.Controls.Remove(this.UserControl);

        // you probably want to replace this with something more sensible
        this.Text = this.UserControl.Text;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.UserControl.Dispose();
        }

        base.Dispose(disposing);
    }
}

【讨论】:

  • 哇,关于如何使用自定义下拉菜单制作 WinForms ComboBox 的大量信息。非常感谢您的反馈!
【解决方案2】:

是的,WPF 中的示例:

<Window x:Class="WpfApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ComboBox Margin="49,61,75,0" Height="25" VerticalAlignment="Top">
            <ComboBox.Items>                
                <ComboBoxItem>
                    <TextBox>TextBox</TextBox>
                </ComboBoxItem>
                <ComboBoxItem>
                    <TextBlock>TextBlock</TextBlock>
                </ComboBoxItem>
                <ComboBoxItem>
                    <Button>Button</Button>
                </ComboBoxItem>
            </ComboBox.Items>
        </ComboBox>
    </Grid>
</Window>

在 Windows 窗体组合框中可能很麻烦。

【讨论】:

【解决方案3】:

不在 Windows 窗体中,但在 WPF 中,您可以将任何内容放入 ComboBox...

【讨论】:

    【解决方案4】:

    是的,如果您使用的是 WPF。

    【讨论】:

      【解决方案5】:

      如果您谈论的是 ASP.NET,那么如果您使用的是标准 ASP.NET 控件,那么答案是否定的。但是,如果您使用 HTML/JavaScript 创建 ComboBox 样式控件,则可以。

      【讨论】:

        【解决方案6】:

        我相信 JMSA 正在谈论 Windows 窗体。

        我不是 .NET 专家,但您可以创建自己的 OwnerDrawn 组合框。 ListBoxs 和其他项目持有控件都有的方法有 OnDrawItem() 和 MeasureItem();它可以被覆盖,你可以完全控制。

        CheckBoxRenderer等是可以在网上找到的类,它接受一个图形对象,可以绘制windows窗体。

        这当然是一个非常漫长的过程:我建议寻找一种更简单的方法来完成您的任务。我也是,有时我有一个疯狂的客户想要华丽的超级组合框,好吧..接受挑战!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-14
          • 2014-01-09
          • 2023-04-06
          • 2011-04-21
          • 1970-01-01
          相关资源
          最近更新 更多