【问题标题】:selecteditem or selectedindex for listpicker列表选择器的 selecteditem 或 selectedindex
【发布时间】:2014-09-11 08:08:42
【问题描述】:

App 打开时,ListPicker 的 selectedItem 即“BackgroundColor”必须来自变量。如何做到这一点?

XAML:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="PickerItemTemplate">
        <TextBlock Text="{Binding BackGroundColorString}" />
    </DataTemplate>
    <DataTemplate x:Name="PickerFullModeItemTemplate" >
        <Grid x:Name="rootGrid" Margin="0">
             <StackPanel Orientation="Horizontal"  Margin="0 14 0 0" HorizontalAlignment="Center">
                <TextBlock Name="BackgroundColor" 
                            Text="{Binding BackGroundColorString}"
                            FontSize="35" 
                            Margin="10,10"                              
                            TextAlignment="Center"
                           FontFamily="/Assets/Fonts/AGENCYR.TTF#Agency FB"
               />
            </StackPanel>
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>        

<toolkit:ListPicker x:Name="BackgroundColor"  FullModeHeader="Select Background Color:" 
                    Header="Background Color:" BorderThickness="0" 
                    FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" 
                    ItemTemplate="{StaticResource PickerItemTemplate}" Background="#FF09043C" 
                    SelectionChanged="BackgroundColor_SelectionChanged" >
 </toolkit:ListPicker>

C#:

public class BackGroundlistPickerClass
{
    public string BackGroundColorString
    {
        get;
        set;
    }
}

List<BackGroundlistPickerClass> BackGroundColorList = new List<BackGroundlistPickerClass>();

public void ImplementListPickeritems() //Listpickers
{
    BackGroundColorList.Add(new BackGroundlistPickerClass() { BackGroundColorString = "White (Default)" });
    BackGroundColorList.Add(new BackGroundlistPickerClass() { BackGroundColorString = "Black" });
    BackGroundColorList.Add(new BackGroundlistPickerClass() { BackGroundColorString = "Light Grey" });

}

string PreSelectedColor="Black";
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    BackgroundColor.SelectedItem=PreSelectedColor; //  ERROR COMES ON THIS LINE
}

BackgroundColor.SelectedItem 不起作用,因为BackgroundColor 中的项目确实来自类/列表。现在如何在页面打开时将 BackgroundColor listpicker 设置为 Black(PreSelectedColor)?

【问题讨论】:

    标签: c# windows-phone-8 indexing selecteditem listpicker


    【解决方案1】:

    您需要将SelectedItem 设置为ItemsSource 中的一个项目。您可以尝试这种方式,假设 BackGroundColorList 属性用于 ItemsSource

    string PreSelectedColor="Black";
    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        var defaultColor = 
                BackGroundColorList.FirstOrDefault(o => o.BackGroundColorString == PreSelectedColor);
        BackgroundColor.SelectedItem = defaultColor;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2012-01-31
      • 2013-05-20
      • 1970-01-01
      相关资源
      最近更新 更多