How to create a LongListSelector control programmatically

元气小坏坏 提交于 2019-12-13 02:16:45

问题


This is yet another LongListSelector question..

I need to have some selectors with this style and add different bindings to them:

<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>

I want to make it programmatically. I saw this answer, but that didn't work for me in windows phone 8.

How can I reproduce it by code or as a style? thanks


回答1:


I hope you know how to bind data in longlistSelector. Assuming that below is the code. Try to Bind it on your own.

    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;
    }


来源:https://stackoverflow.com/questions/17415370/how-to-create-a-longlistselector-control-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!