如何创建具有多个数据模板的列表视图 [英] How to create a listview with multiple datatemplates

查看:94
本文介绍了如何创建具有多个数据模板的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello,
I'm buzzy to create a custom listview witch has custom defined views based on the data it gets. I like
views with links such described in the example below and for example with a textbox and a textblock,
but it doesn't matter how it goes about it's idea.
I have a enum like this to know witch datatemplate must be used:
<pre>
    public enum tabType
    {
        tabbedTextBlock = 1,
        tabbedLinkedBlock = 2
    }


此类是用于将数据存储在
中的类


This class is a class to store data in

public class person
{
    private string firstName;
    private string lastName;
    private int age;
    private tabType type;

    public person()
    {
        // Insert code required on object creation below this point.
    }
    public person(string firstName, string lastName, int age, tabType type)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;
        this.tabType = type;
    }
    public string FirstName
    {
        get;
        set;
    }
    public string LastName
    {
        get;
        set;
    }
    public int Age
    {
        get;
        set;
    }
            public tabType tabType
            {
                    get;
                    set;
    }


对象列表也可以绑定到列表


a list of objects too be bind to the list

public class sampledata : ObservableCollection<person>
{
    public sampledata() :base()
    {
        Add(new person("foo", "fofoo", 23, tabType.tabbedLinkedBlock));
        Add(new person("apple", "fruit", 21, tabType.tabbedTextBlock));
        Add(new person("pineapple", "fruit", 17, tabType.tabbedLinkedBlock));
        Add(new person("banan", "fruit", 47, tabType.tabbedLinkedBlock));
        Add(new person("kiwi", "fruit", 34, tabType.tabbedTextBlock));
    }
}



tabtype用于让自定义listview类知道最常使用的数据模板类型.
我已经搜寻了一个观看日,但是找不到一个好的工作解决方案,只有一个主意,但是
它是用Windows窗体在VB.net中编写的.可以在此处找到.

thx,
DJohn



the tabtype is used to let the custom listview class know witch type of datatemplate most be used.
I have searched for a view day''s but couldn''t found a nice working solution, only an idea but
it''s written in VB.net with windows forms. It can be found here.

thx,
DJohn

推荐答案

我看不出为什么要使用emum来指示要使用哪个数据模板.只需将视图和视图模型绑定即可.




< DataTemplate DataType ="{x:Type vmNameSpace:tabbedLinkedBlock}"
< viewNameSpace:ViewObjectForTheTabbedLinkedBlock/>
</DataTemplate>

< DataTemplate DataType ="{x:Type vmNameSpace:tabbedTextBlock}"
< viewNameSpace:ViewObjectForTheTabbedTextBlock/>
</DataTemplate>

我不确定您希望它们如何显示,所以我对您的其他项目进行了查看,然后您说您已经拥有了.现在,如果它们实际上是不同的项目,则视图将被绑定到这些不同的项目.
I see no reason why you are using an emum to tell which datatemplate to use. Just bind your view and your viewmodel and you are done.


i.e.

<DataTemplate DataType="{x:Type vmNameSpace:tabbedLinkedBlock}"
<viewNameSpace:ViewObjectForTheTabbedLinkedBlock/>
</DataTemplate>

<DataTemplate DataType="{x:Type vmNameSpace:tabbedTextBlock}"
<viewNameSpace:ViewObjectForTheTabbedTextBlock/>
</DataTemplate>

I am not sure how you want them displayed so I made a view for your different items then you are saying you have. Now if they are actually different items the view will get used as it is bound to these different items.


您可以在< ItemTemplate>内使用多视图. MultiView ActiveViewIndex将根据类型进行更改.
示例:

.aspx代码

< asp:ListView ID ="lvList" runat ="server" OnItemDataBound ="lvList_ItemDataBound">
< LayoutTemplate>
< ul runat ="server" id ="ulContainer">
< li runat ="server" id ="itemPlaceholder"></li>
</ul>
</LayoutTemplate>
< ItemTemplate>

< asp:MultiView ID ="mv" runat ="server" ActiveViewIndex ="0">
< asp:查看ID ="v1" runat =服务器">
< asp:TextBox ID ="txt1" runat ="server"></asp:TextBox>
</asp:View>
< asp:View ID ="v2" runat ="server">
< asp:文字ID ="ltl1" runat ="server"></asp:文字>
</asp:View>
</asp:MultiView>
< br/>
</ItemTemplate>
< EmptyDataTemplate>
</EmptyDataTemplate>
</asp:ListView>

C#代码

AA类{
公共AA(int v)
{
p = v;
}
public int p;
}

受保护的void lvList_ItemDataBound(对象发送者,ListViewItemEventArgs e)
{
如果(e.Item.ItemType == ListViewItemType.DataItem)
{
AA参数=(e.Item为ListViewDataItem).DataItem为AA;

如果(param.p == 1)
{
字面量ltl1 = e.Item.FindControl("ltl1")as Literal;
ltl1.Text =样本";
MultiView mv = e.Item.FindControl("mv")as MultiView;
mv.ActiveViewIndex = 1;
}
其他
{
TextBox txt1 = e.Item.FindControl("txt1")as TextBox;
txt1.Text =样本";
MultiView mv1 = e.Item.FindControl("mv")as MultiView;
mv1.ActiveViewIndex = 0;
}
}
}

列出< AA> a = new List< AA>();
a.Add(新AA(1));
a.Add(new AA(2));
lvList.DataSource = a;
lvList.DataBind();
you can use multiview inside the <ItemTemplate>. MultiView ActiveViewIndex will changed accourding to the type.
Example:

.aspx code

<asp:ListView ID="lvList" runat="server" OnItemDataBound="lvList_ItemDataBound">
<LayoutTemplate>
<ul runat="server" id="ulContainer">
<li runat="server" id="itemPlaceholder"></li>
</ul>
</LayoutTemplate>
<ItemTemplate>

<asp:MultiView ID="mv" runat="server" ActiveViewIndex="0">
<asp:View ID="v1" runat="server">
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</asp:View>
<asp:View ID="v2" runat="server">
<asp:Literal ID="ltl1" runat="server"></asp:Literal>
</asp:View>
</asp:MultiView>
<br/>
</ItemTemplate>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:ListView>

C# code

class AA {
public AA(int v)
{
p = v;
}
public int p;
}

protected void lvList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
AA param = (e.Item as ListViewDataItem).DataItem as AA;

if (param.p == 1)
{
Literal ltl1 = e.Item.FindControl("ltl1") as Literal;
ltl1.Text = "sample";
MultiView mv = e.Item.FindControl("mv") as MultiView;
mv.ActiveViewIndex = 1;
}
else
{
TextBox txt1 = e.Item.FindControl("txt1") as TextBox;
txt1.Text = "sample";
MultiView mv1 = e.Item.FindControl("mv") as MultiView;
mv1.ActiveViewIndex = 0;
}
}
}

List<AA> a = new List<AA>();
a.Add(new AA(1));
a.Add(new AA(2));
lvList.DataSource = a;
lvList.DataBind();


这篇关于如何创建具有多个数据模板的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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