繁体   English   中英

C# - .net控件作为ComboBox项

[英]C# - .net controls as ComboBox Items

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

也就是说,当组合被删除时,其每个项目将显示一个文本框。

是的,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窗体中,组合框可能很麻烦。

在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);
    }
}

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

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

我可能会说明显而已,但也有Silverlight。

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

我相信JMSA正在谈论Windows Forms。

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

CheckBoxRenderer等是可以在线找到的类,它们可以绘制图形对象并可以绘制窗体。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM