我有一个项目需要将字符串列表绑定到 listpicker 控件。绑定完成后(在 Loaded 或 OnNavigatedTo 事件中),listpicker 打开以显示绑定的项目,但有些项目随机丢失(在全屏模式下)。当列表选择器开始关闭时,丢失的项目似乎出现了。不知道为什么会发生这种情况 - 是列表选择器错误还是我绑定不正确?我正在使用 2011 年 11 月发布的 silverlight 工具包。
PS:使用少量项目(最多10个)时似乎不会发生此问题
请使用以下代码重新创建问题
主页 XAML
<TextBlock Text="Time of Day" Grid.Row="0" Margin="12,0,0,0" Style="{StaticResource PhoneTextNormalStyle}" />
<toolkit:ListPicker x:Name="TimesOfDayList" Grid.Row="1" SelectionMode="Multiple" FullModeHeader="Time of Day"
ExpansionMode="FullScreenOnly" ItemsSource="{Binding TimesOfDay}"
Margin="12,0,12,12">
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="24,0,0,0" Style="{StaticResource PhoneTextLargeStyle}" />
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
</Grid>
后面的主页代码
private MainPageViewModel _viewModel;
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
TimesOfDayList.SummaryForSelectedItemsDelegate = TimesOfDaySummary;
}
void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
_viewModel = new MainPageViewModel();
DataContext = _viewModel;
}
private string TimesOfDaySummary(IList items)
{
string itemNames = string.Empty;
if (items != null && items.Count > 0)
{
var itemsList = items.Cast<string>().AsQueryable();
foreach (var item in itemsList)
{
if (item != itemsList.Last())
itemNames += item + ", ";
else
itemNames += item;
}
}
return itemNames;
}
视图模型类
public class MainPageViewModel : INotifyPropertyChanged
{
public MainPageViewModel()
{
TimesOfDay = new List<string>
{
"12:00 AM",
"12:30 AM",
"01:00 AM",
"01:30 AM",
"02:00 AM",
"02:30 AM",
"03:00 AM",
"03:30 AM",
"04:00 AM",
"04:30 AM",
"05:00 AM",
"05:30 AM",
"06:00 AM",
"06:30 AM",
"07:00 AM",
"07:30 AM",
"08:00 AM",
"08:30 AM",
"09:00 AM",
"09:30 AM",
"10:00 AM",
"10:30 AM",
"11:00 AM",
"11:30 AM",
"12:00 PM",
"12:30 PM",
"01:00 PM",
"01:30 PM",
"02:00 PM",
"02:30 PM",
"03:00 PM",
"03:30 PM",
"04:00 PM",
"04:30 PM",
"05:00 PM",
"05:30 PM",
"06:00 PM",
"06:30 PM",
"07:00 PM",
"07:30 PM",
"08:00 PM",
"08:30 PM",
"09:00 PM",
"09:30 PM",
"10:00 PM",
"10:30 PM",
"11:00 PM",
"11:30 PM"
};
}
private List<string> _timesOfDay;
public List<string> TimesOfDay
{
get { return _timesOfDay; }
set
{
if(_timesOfDay != value)
{
_timesOfDay = value;
OnPropertyChanged("TimesOfDay");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}