如何在WPF中创建类似UserControl的ComboBox

发布于 2024-10-21 04:34:50 字数 1330 浏览 1 评论 0原文

我正在尝试构建一个能够从 XAML 中获取元素的用户控件,如下所示:

            <ComboBox >
                <ComboBoxItem />
                <ComboBoxItem />
                <ComboBoxItem />
            </ComboBox>

ComboBox 中,您只需在 ComboBox 标记之间添加项目,然后我想复制这个,但不知道从哪里开始。

完成后它应该看起来像这样:

    <cis:ReportControl Grid.Row="3">
            <cis:ReportItem />
    </cis:ReportControl>

在 cis:ReportControl 中,有一些 Button 和一个 ComboBox,基本上我只想提供带有项目的ComboBox

报表项只是一个带有一些额外属性的ComboBoxItem

编辑:

我已经根据@Snowbears的回答实现了它,但现在的问题是控件本身作为一个项目。 我认为这是因为我有一些内容,并且通过将 ContentProperty 定义到我的 ComboBox,它被重定向到 Box 中。 我可以做什么来避免这种情况?

编辑 II:

现在可以完全使用此方法:

    private ItemCollection reportItems;
    public ItemCollection ReportItems
    {
        get
        {
            if (reportItems == null)
            {
                reportItems = this.ComboBoxReports.Items;
            }
            return reportItems;
        }
    }

使用 [ContentProperty("ReportItems")] 属性。 ComboBoxReports 是控件中的组合框,我必须继承 ItemsControl 才能使其工作。

I'm trying to build an usercontrol wich is able to take elements from XAML like this:

            <ComboBox >
                <ComboBoxItem />
                <ComboBoxItem />
                <ComboBoxItem />
            </ComboBox>

In the ComboBox, you can just add the Items between the ComboBox tags, and I would like to copy this, but I don't know where to start.

Finished it should look like this:

    <cis:ReportControl Grid.Row="3">
            <cis:ReportItem />
    </cis:ReportControl>

In the cis:ReportControl, there are some Buttons and a ComboBox, and basically I only want to feed the ComboBox with Items.

The Report Item is just a ComboBoxItem with some extra properties.

Edit:

I've implemented it according to @Snowbears answer, but the problem now is that the control has itself as an item.
I think this is because I have some content, and by defining the ContentProperty to my ComboBox, it is redirected into the Box.
What can I do to avoid this?

Edit II:

It fully works now with this:

    private ItemCollection reportItems;
    public ItemCollection ReportItems
    {
        get
        {
            if (reportItems == null)
            {
                reportItems = this.ComboBoxReports.Items;
            }
            return reportItems;
        }
    }

with the [ContentProperty("ReportItems")] Attribute. ComboBoxReports is the ComboBox in the Control, and I had to inherit from ItemsControl to get it to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤ぢ 2024-10-28 04:34:50
  1. 您应该在 UserControl 中创建属性,该属性将公开实现 IList 接口的内容。假设此属性将命名为 ReportItems。该属性不应具有设置器,并且应在 UserControl 本身中或在支持字段上的字段初始化的构造函数中进行初始化。
  2. UserControl 应使用您的 ContentProperty 属性进行标记属性名称 (ReportItems)
  3. 内部组合框应将其 ItemsSource 绑定到 UserControl 的 ReportItems 属性
  1. You should create property in your UserControl which will expose something implementing IList interface. Let's say this property will be named ReportItems. This property should not have setter and it should be initialized in UserControl itself either in constructor in by field initialization on backing field.
  2. UserControl should be marked with ContentProperty attribute with your property name (ReportItems)
  3. Internal combobox should have it's ItemsSource bound to UserControl's ReportItems property
你与昨日 2024-10-28 04:34:50

如果您寻找如何创建自己的控件,您必须寻找两件事:

  1. 自定义控件 1用户控制 1 (它取决于您的需要
  2. 依赖属性(在控制中使用它们)

我认为您可能需要使用自定义控件。您还可以从 ComboBox 或其他控件继承自定义控件。

if you look for How to Create Your own Control, You must look for two things:

  1. Custom Control 1 or User Control 1 (it's depend on your need)
  2. Dependency Properties (use them in control)

I think you might need to use Custom-Control. Also you can inherit your Custom-Control from ComboBox or other Controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文