如何以编程方式创建LongListSelector控件

本文关键字:创建 LongListSelector 控件 方式 编程 | 更新日期: 2023-09-27 18:20:48

这是另一个LongListSelector问题。。

我需要一些具有这种风格的选择器,并为它们添加不同的绑定:

<phone:LongListSelector x:Name="ListSelector">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel Name="containerStack" Margin="0,0,0,0" Orientation="Horizontal">
                <StackPanel HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="60" Margin="3,20,2,20">
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="White"/>
                </StackPanel>
                <StackPanel Height="Auto" VerticalAlignment="Top" Width="350" Margin="2,20,3,20">
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="White" Margin="0"/>
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="DarkBlue" Margin="0"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

我想以编程方式制作它。我看到了这个答案,但在windowsphone 8中对我不起作用。

我如何通过代码或作为一种样式来复制它?感谢

如何以编程方式创建LongListSelector控件

我希望您知道如何在longlistSelector中绑定数据。假设下面是代码。试着自己绑起来。

    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"">
                        <StackPanel Name=""containerStack"" Margin=""0,0,0,0"" Orientation=""Horizontal"">
                        <StackPanel HorizontalAlignment=""Left"" Height=""Auto"" VerticalAlignment=""Top"" Width=""60"" Margin=""3,20,2,20"">
                        <TextBlock Text=""{Binding text}"" TextWrapping=""Wrap"" Style=""{StaticResource PhoneTextLargeStyle}"" FontSize=""{StaticResource PhoneFontSizeMedium}"" Foreground=""White""/>
                        </StackPanel><StackPanel Height=""Auto"" VerticalAlignment=""Top"" Width=""350"" Margin=""2,20,3,20"">
                        <TextBlock Text=""{Binding text}"" TextWrapping=""Wrap"" Style=""{StaticResource PhoneTextLargeStyle}"" FontSize=""{StaticResource PhoneFontSizeMedium}"" Foreground=""White"" Margin=""0""/>
                        <TextBlock Text=""{Binding text}"" TextWrapping=""Wrap"" Style=""{StaticResource PhoneTextLargeStyle}"" FontSize=""{StaticResource PhoneFontSizeMedium}"" Foreground=""DarkBlue"" Margin=""0""/>
                        </StackPanel>
                        </StackPanel>
                        </DataTemplate>";
        DataTemplate res=null;
        try
        {
            res = (DataTemplate)XamlReader.Load(xaml);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        return res;
    }