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

时间:2013-07-01 23:45:57

标签: c# xaml windows-phone-8 windows-phone longlistselector

这是另一个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>

我想以编程方式制作它。我看到了这个answer,但在Windows Phone 8中这对我没用。

如何通过代码或样式重现它?感谢

1 个答案:

答案 0 :(得分:2)

我希望你知道如何在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;
    }