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

标签 c# xaml windows-phone-8 windows-phone 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>

最佳答案

以不同的方式尝试并在 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/

相关文章:

c# - 在 RasDial 之后获取 IP 地址/接口(interface)号

wpf - 如何使我的网格列始终具有相同的宽度?

.net - 在 Silverlight 中将代码隐藏添加到资源字典时出现 xClassNotDerivedFromElement 错误

c# - Visual Studio 2017 Xamarin 中缺少表单 Xaml 页面

windows-phone-8 - Windows Phone 中 NavigationService.Navigate 和 (Application.Current.RootVisual as PhoneApplicationFrame).Navigate 之间的区别

c# - 不允许在 IsolatedStorageFileStream 上对隔离存储中的 CreateFile 进行操作

c# - 使用 NEST : How to configure analyzers to find partial words? 的 Elasticsearch

c# - 如何使用 GridView 创建每行 4 列的动态表?

c# - 如何处理 ReSharper 的名称建议?

windows-phone-8 - 在 Windows Phone 8 中以编程方式设置系统日期和时间