[英]How to set default selection in listpicker windows phone
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.
我已经尝试了selectedindex = indexnumber,但是它崩溃了。 Below are my code:
下面是我的代码:
c# 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 XAML
<toolkit:ListPicker Name="monthCat" ExpansionMode="FullScreenOnly">
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" FontSize="40" Margin="10"/>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
Try it in different way and Use below code in xaml file: 以其他方式尝试它,并在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>
Use below code in .cs file 在.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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.