如何在listpicker windows phone中设置默认选择

时间:2014-04-25 06:52:34

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

我正在尝试获取当前月份并实际将其设置为listpicker中的默认项目。我怎么想让这种情况发生,因为我不知道应该怎么做。我试过selectedindex = indexnumber但它崩溃了。以下是我的代码:

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>

1 个答案:

答案 0 :(得分:0)

以不同方式尝试并在xaml文件中使用以下代码:

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

在.cs文件中使用以下代码

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();