C# 以编程方式在WPF中添加视图/控件

C# 以编程方式在WPF中添加视图/控件,c#,wpf,xaml,C#,Wpf,Xaml,从数据库加载数据后(假设我们加载了课程),我想在列表中显示它们。我已经了解到,您可以使用UserControl动态添加内容,但示例专门用于动态更改内容,而不是显示结果列表 我想要的是创建一个XAML模板,并对每个列表项实例化一次,然后将其添加到列表中(换句话说,将其添加到StackedPanel)。我怎样才能做到这一点?另外,我应该使用页面还是用户控件?这正是创建的列表框和项控件。ListBox是ItemsControl的增强版,所以我只讨论后者,即基本版 ItemsControl包含一个名为I

从数据库加载数据后(假设我们加载了课程),我想在列表中显示它们。我已经了解到,您可以使用
UserControl
动态添加内容,但示例专门用于动态更改内容,而不是显示结果列表


我想要的是创建一个XAML模板,并对每个列表项实例化一次,然后将其添加到列表中(换句话说,将其添加到
StackedPanel
)。我怎样才能做到这一点?另外,我应该使用
页面
还是
用户控件

这正是创建的
列表框
项控件。ListBox是ItemsControl的增强版,所以我只讨论后者,即基本版

ItemsControl
包含一个名为
ItemsSource
的依赖属性。将集合传递到那里,ItemsControl将生成一个“面板”,其中填充从该源获取的项目的“视图”

默认情况下,ItemsControl使用StackPanel作为面板,但如果需要,可以通过
ItemPanel
属性对其进行更改

默认情况下,ItemsControl使用默认的WPF模板匹配为每个项目生成和显示视图。但是,您可以专门使用
ItemTemplate
属性来设置一个
DataTemplate
,该属性将告诉视图应该是什么样子

例如:

<!-- simple vertical/StackPanel list -->
<ItemsControl ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemTemplate>
        <!-- the template of a single Item -->
        <!-- note that I'm setting it here explicitely -->
        <!-- but the ItemTemplate prop could have been bound, resourcized, etc -->
        <DataTemplate>
            <Border BorderThickness="1">
                <!-- reads a ShoeSize from the items from MyItems -->
                <TextBlock Text="{Binding ShoeSize}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

<!-- simple horizontal/StackPanel list -->
<ItemsControl ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="1">
                <TextBlock Text="{Binding ShoeSize}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


<!-- you can use any panel and any itemtemplate -->
<ItemsControl ItemsSource="{Binding MyXYPoints}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- any Panel is OK! even plain canvas -->
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- just remember that in canvas there's no autolayout -->
            <!-- so you need to set the coords on each item! -->
            <Ellipse Width="2" Height="2"
                     Canvas.Left="{Binding PositionX}"
                     Canvas.Top="{Binding PositionY}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ListBox
本质上是同一个控件,但为您提供了一些“默认”功能,如在项目太多时滚动(使用虚拟化)项目、选择项目等。您可以从ItemsControl获得这些功能,但您需要自己实现或至少配置它们。ListBox已经完成了。但绝对比ItemsControl“重”,所以选择最适合的


无论如何。。我写这篇文章只是因为我今天心情很好。但您确实需要更多地了解ItemsControl和绑定。在WPF中,您几乎从不“从代码手动”将内容放在UI上。实际上我不应该写那么多,因为专家们已经涵盖了所有这些请务必阅读。特别是“我”一章,因为它与你所问的内容完全相关,但我真的建议你全部阅读。它们有点详细,对一些读者来说有点太多了,但它们可以让您了解手头有哪些很棒的工具。

这正是
列表框和
项控件创建的内容。ListBox是ItemsControl的增强版,所以我只讨论后者,即基本版

ItemsControl
包含一个名为
ItemsSource
的依赖属性。将集合传递到那里,ItemsControl将生成一个“面板”,其中填充从该源获取的项目的“视图”

默认情况下,ItemsControl使用StackPanel作为面板,但如果需要,可以通过
ItemPanel
属性对其进行更改

默认情况下,ItemsControl使用默认的WPF模板匹配为每个项目生成和显示视图。但是,您可以专门使用
ItemTemplate
属性来设置一个
DataTemplate
,该属性将告诉视图应该是什么样子

例如:

<!-- simple vertical/StackPanel list -->
<ItemsControl ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemTemplate>
        <!-- the template of a single Item -->
        <!-- note that I'm setting it here explicitely -->
        <!-- but the ItemTemplate prop could have been bound, resourcized, etc -->
        <DataTemplate>
            <Border BorderThickness="1">
                <!-- reads a ShoeSize from the items from MyItems -->
                <TextBlock Text="{Binding ShoeSize}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

<!-- simple horizontal/StackPanel list -->
<ItemsControl ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="1">
                <TextBlock Text="{Binding ShoeSize}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


<!-- you can use any panel and any itemtemplate -->
<ItemsControl ItemsSource="{Binding MyXYPoints}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- any Panel is OK! even plain canvas -->
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- just remember that in canvas there's no autolayout -->
            <!-- so you need to set the coords on each item! -->
            <Ellipse Width="2" Height="2"
                     Canvas.Left="{Binding PositionX}"
                     Canvas.Top="{Binding PositionY}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ListBox
本质上是同一个控件,但为您提供了一些“默认”功能,如在项目太多时滚动(使用虚拟化)项目、选择项目等。您可以从ItemsControl获得这些功能,但您需要自己实现或至少配置它们。ListBox已经完成了。但绝对比ItemsControl“重”,所以选择最适合的


无论如何。。我写这篇文章只是因为我今天心情很好。但您确实需要更多地了解ItemsControl和绑定。在WPF中,您几乎从不“从代码手动”将内容放在UI上。实际上我不应该写那么多,因为专家们已经涵盖了所有这些请务必阅读。特别是“我”一章,因为它与你所问的内容完全相关,但我真的建议你全部阅读。它们有点详细,对一些读者来说有点太多了,但它们可以让您了解手头有哪些很棒的工具。

这正是
列表框和
项控件创建的内容。ListBox是ItemsControl的增强版,所以我只讨论后者,即基本版

ItemsControl
包含一个名为
ItemsSource
的依赖属性。将集合传递到那里,ItemsControl将生成一个“面板”,其中填充从该源获取的项目的“视图”

默认情况下,ItemsControl使用StackPanel作为面板,但如果需要,可以通过
ItemPanel
属性对其进行更改

默认情况下,ItemsControl使用默认的WPF模板匹配为每个项目生成和显示视图。但是,您可以专门使用
ItemTemplate
属性来设置一个
DataTemplate
,该属性将告诉视图应该是什么样子

例如:

<!-- simple vertical/StackPanel list -->
<ItemsControl ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemTemplate>
        <!-- the template of a single Item -->
        <!-- note that I'm setting it here explicitely -->
        <!-- but the ItemTemplate prop could have been bound, resourcized, etc -->
        <DataTemplate>
            <Border BorderThickness="1">
                <!-- reads a ShoeSize from the items from MyItems -->
                <TextBlock Text="{Binding ShoeSize}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

<!-- simple horizontal/StackPanel list -->
<ItemsControl ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="1">
                <TextBlock Text="{Binding ShoeSize}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


<!-- you can use any panel and any itemtemplate -->
<ItemsControl ItemsSource="{Binding MyXYPoints}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- any Panel is OK! even plain canvas -->
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- just remember that in canvas there's no autolayout -->
            <!-- so you need to set the coords on each item! -->
            <Ellipse Width="2" Height="2"
                     Canvas.Left="{Binding PositionX}"
                     Canvas.Top="{Binding PositionY}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>