WPF:在模板中使用一个列表框,该模板将成为另一个列表框的模板 [英] WPF: use a ListBox in a template that will be the template of another ListBox

查看:56
本文介绍了WPF:在模板中使用一个列表框,该模板将成为另一个列表框的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListBox,其ItemSource是一个ObjectDataProvider,它是ObservableCollection的一个实例. ObservableCollection是ObservableCollections的集合. ListBox的ItemTemplate是一个DataTemplate,它为列表框的每个项目创建一个ListBox.为了更好地说明这一点,我尝试在WPF中重新创建纸牌游戏.基本上,您可以用纸牌来创建书籍.拥有有效的书籍后,您可以选择将其制作为书籍,并将其放入ObservableCollection of Books中.我遇到的问题是ListBox的每个项目都是一个ListBox,它的ItemSource是一本书,这是一个ObservableCollection of Cards.我不认为外部ListBox的源或模板有问题,但是我很难理解如何将ListBox项的源设置为卡片的集合每本书.从本质上讲,我的问题可能令人困惑且难以理解,但是从本质上讲,我试图弄清楚如何在将成为另一个ListBox模板的模板中使用ListBox.如果有人对如何解决此问题有任何想法,我将不胜感激.

I have a ListBox whose ItemSource is an ObjectDataProvider that is an instance of an ObservableCollection. The ObservableCollection is a collection of ObservableCollections. The ItemTemplate of the ListBox is a DataTemplate that creates a ListBox for each item of the listbox. To illustrate this better I'm trying to recreate a card game in WPF. Basically, from a hand of cards you can create books. After you have a valid book, you can elect to make it a book which will go into the ObservableCollection of Books. The problem that I'm having is that each item of the ListBox is a ListBox that has an ItemSource that is a Book, that is an ObservableCollection of Cards. I don't think I'm having a problem with the source or the template of the outer ListBox, but I'm having a hard time understanding how I'm going to set the source of the ListBox items to the collection of cards for each book. Essentially, my question may be confusing and a difficult concept to grasp, but essentially I'm trying to figure out how to use a ListBox in a template that will be the template of another ListBox. If anyone has any idea of how to approach this, I would greatly appreciate hearing it.

推荐答案

使用Card,如下所示:

With Card like the following:

 public class Card
{

    private string _name;

    public Card(string name)
    {
        _name = name;
    }


    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

并像下面这样预定:

public class Book
{
    private readonly ObservableCollection<Card> _cards;

    public Book(ObservableCollection<Card> cards)
    {
        _cards = cards;
    }


    public ObservableCollection<Card> Cards
    {
        get { return _cards; }
    }
}

然后在窗口中创建您的ListBox:

Then create your ListBox in the window:

<ListBox
        ItemsSource="{Binding ElementName=Window, Path=Books}"
        ItemTemplate="{StaticResource MainListTemplate}" />

并在窗口的资源中放置:

and in the resources for the window put:

<Window.Resources>
    <ResourceDictionary>

        <DataTemplate
            x:Key="InsideListTemplate">
            <TextBlock
                Text="{Binding Name}" />

        </DataTemplate>

        <DataTemplate
            x:Key="MainListTemplate">
            <ListBox
                ItemsSource="{Binding Cards}"
                ItemTemplate="{StaticResource InsideListTemplate}" />

        </DataTemplate>



    </ResourceDictionary>
</Window.Resources>

您的列表框使用MainListTemplate,其中包含一个ListBox.该ListBox的ItemsSource是您的卡片列表,而ItemTemplate是InsideListTemplate.我将其作为一个简单的TextBlock,但您可以做任何您需要的事情.

Your ListBox uses the MainListTemplate, which contains a ListBox. The ItemsSource for that ListBox is your list of Cards, and the ItemTemplate is the InsideListTemplate. I have it as a simple TextBlock but you could do whatever you need.

这篇关于WPF:在模板中使用一个列表框,该模板将成为另一个列表框的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆