Question

I am trying to get the current month and actually set it as the default item in the listpicker. How am I suppose to make that happen as I have no idea on how i should do it. I have tried selectedindex = indexnumber but it crashed. Below are my code:

c#

var month= DateTime.Now.Month;

monthCat.Items.Add("January");
monthCat.Items.Add("February");
monthCat.Items.Add("March");
monthCat.Items.Add("April");
onthCat.Items.Add("May");
monthCat.Items.Add("June");
monthCat.Items.Add("July");
monthCat.Items.Add("August");
onthCat.Items.Add("September");
monthCat.Items.Add("October");
monthCat.Items.Add("November");
monthCat.Items.Add("December");

//ListPicker.SelectedIndex = month;

xaml

<toolkit:ListPicker Name="monthCat" ExpansionMode="FullScreenOnly">
    <toolkit:ListPicker.FullModeItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" FontSize="40" Margin="10"/>
        </DataTemplate>
    </toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
Was it helpful?

Solution

Try it in different way and Use below code in xaml file:

 <toolkit:ListPicker Width="220" Name="monthCat" VerticalAlignment="Center" Height="58"  ExpansionMode="FullScreenOnly">
                                                <toolkit:ListPicker.ItemTemplate>
                                                    <DataTemplate>
                                                        <StackPanel>
                                                            <TextBlock Text="{Binding}"/>
                                                        </StackPanel>
                                                    </DataTemplate>
                                                </toolkit:ListPicker.ItemTemplate>
                                                <toolkit:ListPicker.FullModeHeader >
                                                    <DataTemplate>
                                                        <TextBlock Text="{Binding}" FontSize="28"/>
                                                    </DataTemplate>
                                                </toolkit:ListPicker.FullModeHeader>
                                            </toolkit:ListPicker>   

Use below code in .cs file

string[] monthName = {"Select", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int month= DateTime.Now.Month;            
monthCat.ItemsSource=monthName;   
monthCat.SelectedIndex = month;
monthCat.UpdateLayout();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top