我正在尝试获取当前月份并将其实际设置为列表选择器中的默认项。我应该如何做到这一点,因为我不知道我应该怎么做。我试过 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>
最佳答案
以不同的方式尝试并在 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();
关于c# - 如何在listpicker windows phone中设置默认选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23286113/