C# Listpicker在设置所选索引后变为空白
我的wp7应用程序中有listpicker控件。我想根据需要设置所选索引。假设我的listpicker中有100个项目,如果我将所选索引设置为40以下,则运行良好。但当我将所选索引设置为50以上时,它会变为空白,UI不会刷新,但在后端它会显示正确的项目 样本项目: 在这个项目中,你可以得到C# Listpicker在设置所选索引后变为空白,c#,windows-phone-7,xaml,windows-phone-7.1,silverlight-toolkit,C#,Windows Phone 7,Xaml,Windows Phone 7.1,Silverlight Toolkit,我的wp7应用程序中有listpicker控件。我想根据需要设置所选索引。假设我的listpicker中有100个项目,如果我将所选索引设置为40以下,则运行良好。但当我将所选索引设置为50以上时,它会变为空白,UI不会刷新,但在后端它会显示正确的项目 样本项目: 在这个项目中,你可以得到 所有来源 还要测试XAP文件 复制步骤 错误快照 只需玩我的最后两个按钮,你就可以很容易地重现这个问题 我正在使用windows phone 7.1.1 SDK和Silverlight工具包2011年11月版
DLL也在我的文件夹中,我在我的项目中引用了它我也遇到过同样的问题。恐怕我还不能想出一个解决办法,但我已经把问题缩小了一点。我可以确认问题似乎发生在
SelectedIndex
40和50之间(在我的例子中是48)
我所做的只是创建一个新的WP解决方案,在MainPage.xaml
视图中添加两个ListPicker
控件,再加上一个按钮。我通过代码将50个字符串添加到两个列表中,并在第一个列表中将SelectedIndex
设置为0,在第二个列表中将其设置为50
该按钮对SelectedIndex属性进行简单切换,如下所示:
private void button1_Click(object sender, RoutedEventArgs e)
{
int tempindex = listPicker1.SelectedIndex;
listPicker1.SelectedIndex = listPicker2.SelectedIndex;
listPicker2.SelectedIndex = tempindex;
}
我主持了这个项目,并以这样的方式结束了(我想,这部电影说明了一切):
我解决了你的问题。我所做的是,我将ListPicker Itemsource绑定到.xaml上,而不是绑定到后面的代码上,它工作得非常好 这是在您提供的文件中编辑的.xaml代码:
<toolkit:ListPicker ItemsSource="{Binding LstCountry}" SelectedIndex="55" x:Name="listPickerCountrySignup" Height="72" HorizontalAlignment="Left" Margin="14,43,0,0" VerticalAlignment="Top" Width="436" FullModeHeader="Select Country" Background="White" BorderBrush="White" CacheMode="BitmapCache" >
.xaml.cs代码
公共部分类主页面:PhoneApplicationPage,INotifyPropertyChanged
{
//建造师
公共主页()
{
初始化组件();
BindList();
this.DataContext=this;
}
public class country
{
public int CountryID { get; set; }
}
public event PropertyChangedEventHandler PropertyChanged;
void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
List<country> _lstCountry;
public List<country> LstCountry
{
get{return _lstCountry;}
set{
if(_lstCountry!=value)
{
_lstCountry = value;
NotifyPropertyChanged("LstCountry");
}
}
}
void BindList()
{
LstCountry = new List<country>();
for (int i = 0; i <= 100; i++)
{
LstCountry.Add(new country { CountryID = i });
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
listPickerCountrySignup.SelectedIndex = 15;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
listPickerCountrySignup.SelectedIndex = 25;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
listPickerCountrySignup.SelectedIndex = 39;
}
private void button4_Click(object sender, RoutedEventArgs e)
{
listPickerCountrySignup.SelectedIndex = 55;
}
private void button5_Click(object sender, RoutedEventArgs e)
{
listPickerCountrySignup.SelectedIndex = 75;
}
}
公共类国家
{
public int CountryID{get;set;}
}
公共事件属性更改事件处理程序属性更改;
void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
列出所有国家;
国家公开名单
{
获取{return\lstu country;}
设置{
如果(lstCountry!=值)
{
_国家=价值;
通知财产变更(“LST国家”);
}
}
}
void BindList()
{
LstCountry=新列表();
对于(int i=0;我请将您的问题缩小到可能的最小大小,但仍然包含问题。不,我不知道您的来源的大小,但链接到它使我相信它不适合这里-这是一个好迹象,它不是一个缩小的问题。另外,无意冒犯,但我不太倾向于在今天和这个时代单击随机链接…@lc.it这只是一个页面,它不超过1.5 MB,因为它包含silverlight toolkit dll,我正在使用它来更好地纠正问题。我刚刚上传了示例代码,因为silverlight tookkit有很多版本和更改集。我正在使用11月的verion官方版本。我解决了这个问题。这是silverl的listpicker控件中的一个bugight toolkit。我更改了toolkit的源代码并修复了它。我现在在移动设备上时会立即共享:)如果您能共享源代码,我会很高兴!我已经在xaml上设置了itemSource绑定,而不是在xaml.cs上,它起了作用,请检查其他答案。我希望它也对您起作用