【问题标题】:How to set default selection in listpicker windows phone如何在 listpicker windows phone 中设置默认选择
【发布时间】:2014-04-25 06:52:34
【问题描述】:

我正在尝试获取当前月份并将其设置为列表选择器中的默认项目。我想怎么做到这一点,因为我不知道我应该怎么做。我试过 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>

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • @Gene Lim 用你的代码替换这段代码,它真的可以工作。
    • 我将自己的 xaml 与提供的 xaml 一起使用。是的,现在它可以工作了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2020-06-14
    • 2017-12-11
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多