如何在 WPF 自定义控件中填充集合控件?

发布于 2024-11-07 12:28:17 字数 288 浏览 0 评论 0原文

我正在学习在 WPF 中创建自定义控件的细节。到目前为止我理解的概念:

  1. 代码定义了控件的行为。
  2. 模板定义了控件的外观和感觉以及视觉方面。
  3. 您可以将模板的元素绑定到基础控制对象的属性。

如果模板有多个集合控件,例如 StackPanels,如何绑定它们以便由底层集合填充它们?我的第一个想法是利用每个 StackPanelDataContext,但我无法让它工作。我感觉我缺少一个可以为我解决这个问题的关键概念。

I'm learning the ins and outs of creating custom controls in WPF. The concepts I understand thus far:

  1. The code defines the control's behavior.
  2. The template defines the control's look and feel, the visual aspect.
  3. You can bind elements of the template to properties of the underlying control object.

If the template has multiple collection controls, such as StackPanels, how do you bind them so that they are populated by underlying collections? My first inclination was to make use of the DataContext of each StackPanel, but I couldn't make it work. I get the feeling I'm missing a key concept that would solve this for me.

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

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

发布评论

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

评论(2

绮烟 2024-11-14 12:28:17

您希望在这些 StackPanel 中显示什么?面板用于排列项目。如果要显示项目集合,您可能需要使用 ItemsControl(或多种类型的 ItemsControl 之一)。 ItemsControl 非常强大 - 您可以指定项目的显示方式以及显示它们的面板的显示方式。您甚至可以指定该面板是 StackPanel。例如,

    <ItemsControl ItemsSource="{Binding ElementName=root, Path=List1}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <!-- Template defines the panel -->
                <StackPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <!-- Template defines each item -->
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

然后您想要绑定到项目列表,这使用 ItemsControl 非常容易!对于自定义控件,您可能希望在代码隐藏中公开控件本身的依赖属性,然后绑定到 XAML 中的依赖属性。例如,您可以为您拥有的各种列表创建依赖属性,如下所示:

        public static readonly DependencyProperty List1Property = DependencyProperty.Register(
        "List1",
        typeof(IList<string>),
        typeof(MyControl));

    public static readonly DependencyProperty List2Property = DependencyProperty.Register(
        "List2",
        typeof(IList<string>),
        typeof(MyControl));

然后您可以绑定 ItemsControls 的 ItemsSource 属性:(

    <ItemsControl ItemsPanel="..." ItemsSource="{Binding ElementName=root, Path=List1}" />
    <ItemsControl ItemsPanel="..." ItemsSource="{Binding ElementName=root, Path=List2}" />

在本例中,我假设自定义控件具有 ax:Name="root")

我希望这样有帮助!

What did you want to be in these StackPanels? Panels are used for arranging items. If you want to show a collection of items, you probably want to be using an ItemsControl (or one of the many types of ItemsControls). An ItemsControl is very powerful- you can specify how the items are displayed and how the panel that they're displayed on is displayed as well. You can even specify that the panel is a StackPanel. For example,

    <ItemsControl ItemsSource="{Binding ElementName=root, Path=List1}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <!-- Template defines the panel -->
                <StackPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <!-- Template defines each item -->
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

Then you wanted to bind to a list of items, which is very easy with an ItemsControl! In your case with the custom control, you may want to expose dependency properties on the control itself in the code-behind and then bind to those from the XAML. For example, you can create dependency properties for the various lists you have like this:

        public static readonly DependencyProperty List1Property = DependencyProperty.Register(
        "List1",
        typeof(IList<string>),
        typeof(MyControl));

    public static readonly DependencyProperty List2Property = DependencyProperty.Register(
        "List2",
        typeof(IList<string>),
        typeof(MyControl));

Then you can bind the ItemsSource property of your ItemsControls:

    <ItemsControl ItemsPanel="..." ItemsSource="{Binding ElementName=root, Path=List1}" />
    <ItemsControl ItemsPanel="..." ItemsSource="{Binding ElementName=root, Path=List2}" />

(in this case I assume that the custom control has a x:Name="root")

I hope this helps!

天赋异禀 2024-11-14 12:28:17

您无法直接绑定 StackPanel 的内容。它只是一个布局控件。

但是,您可以做的是使用 ListBox 并将其 ItemsPanel 设置为 StackPanel (或您需要的任何其他布局控件)。然后,您可以将 ListBoxItemsSource 设置为您想要的任何底层集合。

You can't bind the contents of a StackPanel directly. It's simply a layout control.

What you could do, however, is use a ListBox and set its ItemsPanel to be a StackPanel (or whatever other layout control you need). Then you can set the ListBox's ItemsSource to whatever underlying collection you want.

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