自定义面板提供数据模板和非数据模板

本文关键字:数据 提供数据 自定义 | 更新日期: 2023-09-27 18:16:07

我想创建一个面板,我将能够使用它像下面的例子。

示例1:

<custom:CustomPanel ItemsSource="{Binding DataSource}">
    <custom:CustomPanel.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </custom:CustomPanel.ItemTemplate>
</custom:CustomPanel>

示例2:

<custom:CustomPanel>
    <TextBlock Text="text1" />
    <TextBlock Text="text2" />
    <TextBlock Text="text3" />
</custom:CustomPanel>

我实现了第一个,我现在可以正确地使用它。我通过继承ItemsControl的类来提供它,如下所示。

class CustomPanel : ItemsControl{
    ...
}

我如何提供第二个例子工作?

自定义面板提供数据模板和非数据模板

对于第二个,你需要从ContentControl继承