我想创建一个windows phone
屏幕,显示一些用户信息,listbox
如下所示
现在我正在使用以下代码执行此操作,它工作正常。
<ListBox>
<ListBoxItem Height="100">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="Assets/Users/dummyUser2.png" Grid.RowSpan="2" Grid.Column="0" Height="90" Width="90" />
<TextBlock Text="John Smith" Style="{StaticResource ProfileNameStyleForTextBlock}" />
<TextBlock Text="Birthday" Style="{StaticResource EventNameStyleForTextBlock}" />
<TextBlock Text="Today" Style="{StaticResource EventDateStyleForTextBlock}" Foreground="#09aba9" />
</Grid>
</ListBoxItem>
...........
......
</ListBox>
我想要做的是,我想从后面的代码动态地将项目加载到列表框中,并且视图应该看起来像上图。
是否可以创建这样的样式/控件并将其添加到我将来创建的所有列表中。
编辑
我的列表框
<ListBox Name="lstlist" ItemTemplate="{StaticResource GuyDataTemplate}" />
数据模板
<DataTemplate x:Key="GuyDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="Assets/Users/dummyUser2.png" Grid.RowSpan="2" Grid.Column="0" Height="90" Width="90" />
<TextBlock Text="{Binding Name}" Style="{StaticResource ProfileNameStyleForTextBlock}" />
<TextBlock Text="{Binding Event}" Style="{StaticResource EventNameStyleForTextBlock}" />
<TextBlock Text="{Binding Date}" Style="{StaticResource EventDateStyleForTextBlock}" Foreground="#09aba9" />
</Grid>
</DataTemplate>
我创建了一个类
public class Guy
{
public string Name { get; set; }
public string Event { get; set; }
public string Date { get; set; }
}
并将逻辑写在buttonclick
ObservableCollection<Guy> Guys = new ObservableCollection<Guy>();
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
lstlist.ItemsSource = Guys;
添加此应用后卡住了