如何填充项目模板中的列表框

发布于 2024-10-14 05:00:36 字数 346 浏览 3 评论 0原文

我刚刚学习 XAML 和 Windows Phone 7 编程。 我正在尝试为 WP7 Pivot Control 创建项目模板。我能够制作一个包含列表框的模板。是否可以在代码隐藏中访问此列表框,以便我可以根据自定义类的集合填充它?基本上它的工作原理是我有一个枢轴控件,并且该控件中的每个项目都是一个类别。对于添加的每个类别,都有一个属于该类别的项目列表。我需要能够使用该类别的项目填充每个枢轴项目上的列表。

我搜索了有关如何实现此目的的想法,并且获得了很多有关数据绑定的示例,但我不太熟悉数据绑定在 XAML 中的工作原理。

数据绑定是可行的方法还是我可以以某种方式获取对列表框的引用并自己添加项目? 任何帮助将不胜感激!

谢谢

I am just learning XAML and programming for Windows Phone 7.
Im trying to create an itemtemplate for a WP7 Pivot Control. I was able to make a template which contains a listbox. Is it possible to access this listbox in the code-behind so I can fill it based on a collection of a custom class? Basically how it works is that I have a pivot control and each item in that control is a category. For each category thatis added, there is a list of items that belong to that category. I need to be able to populate the list on each pivot item with items of that category.

I searched for ideas on how to accomplish this, and I get a lot of examples on databinding, but Im not too familiar on how databinding works in XAML.

Would databinding be the way to go or can I somehow get a reference to the listbox and add the items myself?
Any help would be greatly appriciated!

Thank you

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

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

发布评论

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

评论(2

瑾兮 2024-10-21 05:00:36

我对这个主题有一些考虑:

1)如果您通过绑定填充类别列表,那么您就没有保证完成绑定的入口点(因为绑定以延迟方式执行)。

2) 与 DataTemplate 方法相比,使用 ItemTemplate 的内容更加棘手和不可靠,您应该仅在独占情况下使用它。 LogicalTreeHelper 和 VisualTreeHelper 类将为您提供帮助。

3) 但我建议您基于 DataTemplates 构建视图,因为这是 WPF 中的常见做法。你真的认为这段代码相当复杂吗?

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Window.Resources>
    <DataTemplate x:Key="InnerItemDataTemplate">
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>

    <DataTemplate x:Key="CategoryDataTemplate">
        <StackPanel>
            <ListView ItemsSource="{Binding InnerItems}"
                      ItemTemplate="{StaticResource InnerItemDataTemplate}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListView ItemsSource="{Binding Categories}"
              ItemTemplate="{StaticResource CategoryDataTemplate}"/>
</Grid>

public class Category
{
    public IEnumerable<InnerItem> InnerList
    {
        get{/*...*/}
    }
}

class InnerItem
{
    public string Name
    {
        get{/*...*/}
    }
}

public class SampleModel
{
    public IEnumerable<Category> Categories
    {
        get {/*...*/}
    }
}  

I've some considerations on subject:

1) If you fill Categories list via binding, then you don't have entry point where binding is guaranteed comleted (because binding executes in deferred fashion).

2) Working with ItemTemplate's content is more tricky and unreliable, than DataTemplate approach, and you should use it just in exclusive situations. LogicalTreeHelper and VisualTreeHelper classes will help you.

3) But I would recommend you to build your view based on DataTemplates as it is common practice in WPF. Do you really think that this code is pretty complicated?

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Window.Resources>
    <DataTemplate x:Key="InnerItemDataTemplate">
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>

    <DataTemplate x:Key="CategoryDataTemplate">
        <StackPanel>
            <ListView ItemsSource="{Binding InnerItems}"
                      ItemTemplate="{StaticResource InnerItemDataTemplate}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListView ItemsSource="{Binding Categories}"
              ItemTemplate="{StaticResource CategoryDataTemplate}"/>
</Grid>

public class Category
{
    public IEnumerable<InnerItem> InnerList
    {
        get{/*...*/}
    }
}

class InnerItem
{
    public string Name
    {
        get{/*...*/}
    }
}

public class SampleModel
{
    public IEnumerable<Category> Categories
    {
        get {/*...*/}
    }
}  
深海不蓝 2024-10-21 05:00:36

如果您创建一个新的“Windows Phone Pivot 应用程序”,默认代码将显示此示例,但会在多个数据透视项中重用列表框中的相同项目。

以下概述了该示例代码的用途以及如何更改它。

在 MainPage 的构造函数中,DataContext 设置为一个对象 (App.ViewModel)。

MainPage 的此 Loaded 事件可确保填充 App.ViewModel

App.ViewModelMainViewModel 的实例。

MainViewModel 包含一个名为“Items”的 ObservableCollection。正是它绑定到 PivotItem 中的单个 ListBox

<controls:PivotItem Header="first">
    <ListBox ItemsSource="{Binding Items}">
        ...
    </ListBox>
</controls:PivotItem>

在 ListBox 中,您可以引用“Items”集合的内容。

如果您想调整它以使每个 ListBox/PivotItem 具有不同的集合,您只需调整它即可在 MainViewModel 中具有多个集合。

HTH。

IF you create a new "Windows Phone Pivot Application" the default code shows an example of this but reuses the same items in the listbox in multiple pivotitems.

Here's an overview of what that sample code is doing and how you might go about changing it.

In the constructor of MainPage, the DataContext is set to an object (App.ViewModel).

This Loaded event of MainPage ensures that App.ViewModel is populated.

App.ViewModel is an instance of MainViewModel.

MainViewModel contains an ObservableCollection called "Items". It is this that is bound to an idividual ListBox in a PivotItem:

<controls:PivotItem Header="first">
    <ListBox ItemsSource="{Binding Items}">
        ...
    </ListBox>
</controls:PivotItem>

Within the ListBox, you can refer to the contents of the "Items" collection.

If you wanted to adjust this to have different collections for each ListBox/PivotItem you could just adjust this to have multiple collections in the MainViewModel.

HTH.

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