Windows Phone ListBox(内部ListBox) [英] Windows Phone ListBox (inner ListBox)
问题描述
这里我的问题是:
i)列表框1有点项目点击(点击)列表框项目另一个listbox2添加到那里并显示ListBox2(如一个内部列表)项目
如果我点击第一个ListBox的另一个项目我们打开的第二个listBox是关闭的并为该项目打开一个新的相对Listbox2。
你可以使用嵌套的列表框...
看看这个...
http:// debugmode。 net / 2011/08/20 / nested-listbox-binding-in-silverlight-and-windows-phone-7 / [ ^ ]
这是一个样本...使用这个...希望它有帮助...
<前一个=HTML> < 数组listB ox x:name = TeamListbox horizontalalignment = 左 高度 = 自动 verticalalignment = Top width = 自动 style = {StaticResource ListBoxStyle1} itemtemplate = {StaticResource DataTemplate1} xmlns:x = #unknown >
< / listbox >
datatemplate的定义是
< phone:phoneapplicationpage.resources xmlns:phone = #unknown >
< datatemplate x:key = DataTemplate2 xmlns:x = #unknown >
< grid >
< textblock margin = 0,0,1,0 fontsize = < span class =code-keyword> 25 前景 = 绿色 fontweight = SemiBold textwrapping = 换行 text = {Binding} d:layoutoverrides = 宽度,高度 xmlns:d = #unknown / >
< / grid >
< / datatemplate >
< datatemplate x:key = DataTemplate1 xmlns:x = #unknown >
< stackpanel >
< textbl ock margin = 0,0,1,0 fontsize = 32 foreground = 白色 fontweight = 粗体 textwrapping = 换行 text = {Binding TeamName} d:layoutoverrides = 宽度,高度 点击 = TextBlock_Tap xmlns:d = #unknown / >
< listbox x:name = InnerLsitbox tag < span class =code-keyword> = {Binding TeamName} itemtemplate = {StaticResource DataTemplate2} itemssource = {Binding TeamMembers} visibility = Collapsed / >
< / stackpanel >
< / datatemplate >
< / phone:phoneapplicationpage.resources >
在代码隐藏中,
定义自己的类
公开 类团队
{
public string TeamName { get ; set ; }
public List< string> TeamMembers { get ; set ; }
}
< / string >
初始化集合并填充一些虚拟数据
ObservableCollection< teams>团队;
private FrameworkElement expandedElement;
public MainPage()
{
InitializeComponent();
teams = new ObservableCollection< teams>();
InitializeTeams();
TeamListbox.ItemsSource = teams;
}
public void InitializeTeams()
{
teams.Add( new Teams(){TeamName = India,TeamMembers = new List< string>(){ aaaaa1, sssssssss1, ddddd1, fffffffffff1, zzzzzzz1, ccccc1}});
teams.Add( new 团队(){TeamName = Australia,TeamMembers = new 列表< string>(){ aaaaa2, sssssssss2,< span class =code-string> ddddd2, fffffffffff2, zzzzzzz2, ccccc2}});
teams.Add( new 团队(){TeamName = 斯里兰卡,TeamMembers = new 列表< string>(){ aaaaa3, sssssssss3,< span class =code-string> ddddd3, fffffffffff3, zzzzzzz3, ccccc3}});
teams.Add( new 团队(){TeamName = 南非,TeamMembers = new 列表< string>(){ aaaaa4, sssssssss4, ddddd4, fffffffffff4, zzzzzzz4, ccccc4}});
teams.Add( new 团队(){TeamName = England,TeamMembers = new 列表< string>(){ aaaaa5, sssssssss5,< span class =code-string> ddddd5, fffffffffff5, zzzzzzz5, ccccc5}});
teams.Add( new 团队(){TeamName = West Indies,TeamMembers = new List< string>(){ aaaaa6, sssssssss6, ddddd6, fffffffffff6, zzzzzzz6, ccccc6}});
}
< / string > < / string > ; < / string > < / string > < / string > < span class =code-keyword>< / string > < / 团队 > < / 团队 >
在点击事件中,像这样处理innerListbox的可见性
私有 void TextBlock_Tap( object sender,GestureEventArgs e)
{
if (expandedElement!= null )
(expandedElement as ListBox).Visibility = System.Windows.Visibility.Collapsed;
ListBox ll =(((sender as TextBlock).Parent as StackPanel).Children [ 1 ] as ListBox);
expandedElement = ll;
ll.Visibility = System.Windows.Visibility.Visible;
}
Here my problem is :
i) List Box 1 has it items when ever click(Tap) the listbox item another listbox2 add to there and show ListBox2(as a inner list) items
if i click the another item of 1st ListBox what ever we open 2nd listBox is closed and open new one relative Listbox2 for that item .
you can use nested listboxes...
have a look at this...
http://debugmode.net/2011/08/20/nested-listbox-binding-in-silverlight-and-windows-phone-7/[^]
Here is a sample... use this...hope it helps...
<listbox x:name="TeamListbox" horizontalalignment="Left" height="Auto" verticalalignment="Top" width="Auto" style="{StaticResource ListBoxStyle1}" itemtemplate="{StaticResource DataTemplate1}" xmlns:x="#unknown"> </listbox>
The definitions for datatemplate is
<phone:phoneapplicationpage.resources xmlns:phone="#unknown"> <datatemplate x:key="DataTemplate2" xmlns:x="#unknown"> <grid> <textblock margin="0,0,1,0" fontsize="25" foreground="Green" fontweight="SemiBold" textwrapping="Wrap" text="{Binding }" d:layoutoverrides="Width, Height" xmlns:d="#unknown" /> </grid> </datatemplate> <datatemplate x:key="DataTemplate1" xmlns:x="#unknown"> <stackpanel> <textblock margin="0,0,1,0" fontsize="32" foreground="White" fontweight="Bold" textwrapping="Wrap" text="{Binding TeamName}" d:layoutoverrides="Width, Height" tap="TextBlock_Tap" xmlns:d="#unknown" /> <listbox x:name="InnerLsitbox" tag="{Binding TeamName}" itemtemplate="{StaticResource DataTemplate2}" itemssource="{Binding TeamMembers}" visibility="Collapsed" /> </stackpanel> </datatemplate> </phone:phoneapplicationpage.resources>
In code-behind,
define your own class
public class Teams { public string TeamName { get; set; } public List<string> TeamMembers { get; set; } } </string>
Initilalise the collection and fill with some dummy data
ObservableCollection<teams> teams ; private FrameworkElement expandedElement; public MainPage() { InitializeComponent(); teams = new ObservableCollection<teams>(); InitializeTeams(); TeamListbox.ItemsSource = teams; } public void InitializeTeams() { teams.Add(new Teams() { TeamName = "India", TeamMembers= new List<string> () { "aaaaa1", "sssssssss1", "ddddd1", "fffffffffff1", "zzzzzzz1", "ccccc1" } }); teams.Add(new Teams() { TeamName = "Australia", TeamMembers = new List<string>() { "aaaaa2", "sssssssss2", "ddddd2", "fffffffffff2", "zzzzzzz2", "ccccc2" } }); teams.Add(new Teams() { TeamName = "Srilanka", TeamMembers = new List<string>() { "aaaaa3", "sssssssss3", "ddddd3", "fffffffffff3", "zzzzzzz3", "ccccc3" } }); teams.Add(new Teams() { TeamName = "South Africa", TeamMembers = new List<string>() { "aaaaa4", "sssssssss4", "ddddd4", "fffffffffff4", "zzzzzzz4", "ccccc4" } }); teams.Add(new Teams() { TeamName = "England", TeamMembers = new List<string>() { "aaaaa5", "sssssssss5", "ddddd5", "fffffffffff5", "zzzzzzz5", "ccccc5" } }); teams.Add(new Teams() { TeamName = "West Indies", TeamMembers = new List<string>() { "aaaaa6", "sssssssss6", "ddddd6", "fffffffffff6", "zzzzzzz6", "ccccc6" } }); } </string></string></string></string></string></string></teams></teams>
In the tap event, handle the visibility of innerListbox like this
private void TextBlock_Tap(object sender, GestureEventArgs e) { if (expandedElement != null) (expandedElement as ListBox).Visibility = System.Windows.Visibility.Collapsed; ListBox ll = (((sender as TextBlock).Parent as StackPanel).Children[1] as ListBox); expandedElement = ll; ll.Visibility = System.Windows.Visibility.Visible; }
这篇关于Windows Phone ListBox(内部ListBox)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!