2

我是开发新手,我正在尝试使用 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# 创建它的建议都很好,或者任何与我的答案相关的链接。

谢谢 :)

4

3 回答 3

3

您可以通过在后面的代码中显式应用样式来做到这一点。首先用预定义的键定义 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;
于 2013-04-10T07:11:40.520 回答
1

你看这里了吗?

windows phone 8 上“LongListSelector”的官方微软链接:

wp7 和 wp8 上来自“CodeProject”的模板项目:

我希望这有帮助

于 2013-04-10T07:09:02.480 回答
0

自己绑定。试试这个。

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;
}
于 2013-07-03T11:51:19.430 回答