C# 使用DataTemplate选择ListPicker的SelectedIndex或SelectedItem

C# 使用DataTemplate选择ListPicker的SelectedIndex或SelectedItem,c#,windows-phone-8,invalidoperationexception,selectedindex,listpicker,C#,Windows Phone 8,Invalidoperationexception,Selectedindex,Listpicker,我使用Listpicker允许用户选择颜色。 所以我使用了一个工具箱Listpicker和一个包含Textbox的DataTemplate来显示它的列表项。我想要的是,当页面加载时,先前选择的颜色(项目)会自动选择。但它给了我一个明显的“System.InvalidOperationException”异常,因为这些项不是简单地添加的,而是通过datatemplate文本框添加的。请帮助我: 实际上,在简化我的代码时: BackgroundColor.SelectedIndex=0;不起作用

我使用Listpicker允许用户选择颜色。 所以我使用了一个工具箱Listpicker和一个包含Textbox的DataTemplate来显示它的列表项。我想要的是,当页面加载时,先前选择的颜色(项目)会自动选择。但它给了我一个明显的“System.InvalidOperationException”异常,因为这些项不是简单地添加的,而是通过datatemplate文本框添加的。请帮助我:



实际上,在简化我的代码时: BackgroundColor.SelectedIndex=0;不起作用 也不是 背景色。选择editem=“红色”

请帮助我设置项目

This.BackgroundColor.SelectedItem = YourObject;
这就是如何获取ListPicker的selecteditem,可能需要强制转换到绑定到ListPicker的对象

    private void BackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      var item = (sender as ListPicker).SelectedItem;
    }

你的问题不清楚!您想做什么?如何设置具有DataTemplate的ListPicker的SelectedItem?您的ItemsSource是什么?
This.BackgroundColor.SelectedItem = YourObject;
    private void BackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      var item = (sender as ListPicker).SelectedItem;
    }