如何用c#代码创建LongListSelector
本文关键字:创建 LongListSelector 代码 何用 | 更新日期: 2023-09-27 18:08:17
我是开发新手,我试图用c#代码创建LongListSelector,但没有成功。谁能帮我创建这个?
我的XML代码是-
<phone:LongListSelector ItemRealized="LLS_ItemRealized" Name="longList" ItemsSource="{Binding supportedList}" IsGroupingEnabled="False" LayoutMode="List" HideEmptyGroups="True" Margin="30,0,26,0" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="210"/>
<ColumnDefinition Width="210"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
</Grid.RowDefinitions>
<Image Height="118" Width="209" Tap="ThumbnailImageTap" Margin="0" Stretch="Fill" Source="{Binding ThumbnailUrl1}" Tag="{Binding ClipId1}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<Image Height="118" Width="209" Tap="ThumbnailImageTap" Margin="0" Stretch="Fill" Source="{Binding ThumbnailUrl2}" Tag="{Binding ClipId2}" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
任何用c#创建这个的建议都会很棒,或者任何与我的答案相关的链接。
谢谢:)
可以通过在后面的代码中显式地应用样式来实现这一点。首先用预定义的键定义LongListSelector。
<phone:LongListSelector x:Key="listSelector" ItemRealized="LLS_ItemRealized" Name="longList" ItemsSource="{Binding supportedList}" IsGroupingEnabled="False" LayoutMode="List" HideEmptyGroups="True" Margin="30,0,26,0" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="210"/>
<ColumnDefinition Width="210"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
</Grid.RowDefinitions>
<Image Height="118" Width="209" Tap="ThumbnailImageTap" Margin="0" Stretch="Fill" Source="{Binding ThumbnailUrl1}" Tag="{Binding ClipId1}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<Image Height="118" Width="209" Tap="ThumbnailImageTap" Margin="0" Stretch="Fill" Source="{Binding ThumbnailUrl2}" Tag="{Binding ClipId2}" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
然后在后面的代码中获得样式,并应用到新创建的LongListSelector。
var listSelector=new LongListSelector();
listSelector.Style=Resources["listSelector"] as Style;
你看这里了吗?
windows phone 8上"LongListSelector"的官方微软链接:
- http://code.msdn.microsoft.com/wpapps/peoplehub - windows -电话- 80 - 88 - abe94d
wp7和wp8上的"CodeProject"模板项目:
- http://www.codeproject.com/Articles/92439/Silverlight-DataTemplateSelector
希望对您有所帮助
自行绑定。试试这个
LongListSelector listSelector;
private void CreateLongListSelector()
{
listSelector = new LongListSelector()
{
HideEmptyGroups=false,
IsGroupingEnabled=false,
};
ContentPanel.Children.Add(listSelector);
listSelector.ItemTemplate = GetDataTemplate();
}
public DataTemplate GetDataTemplate()
{
string xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<Grid Background=""Transparent"">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""210""/>
<ColumnDefinition Width=""210""/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=""120""/>
</Grid.RowDefinitions>
<Image Height=""118"" Width=""209"" Tap=""ThumbnailImageTap"" Margin=""0"" Stretch=""Fill"" Source=""{Binding ThumbnailUrl1}"" Tag=""{Binding ClipId1}"" Grid.Column=""0"" VerticalAlignment=""Center"" HorizontalAlignment=""Left""/>
<Image Height=""118"" Width=""209"" Tap=""ThumbnailImageTap"" Margin=""0"" Stretch=""Fill"" Source=""{Binding ThumbnailUrl2}"" Tag=""{Binding ClipId2}"" Grid.Column=""1"" VerticalAlignment=""Center"" HorizontalAlignment=""Right""/>
</Grid>
</DataTemplate>";
DataTemplate res=null;
try
{
res = (DataTemplate)XamlReader.Load(xaml);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return res;
}