在没使用mvvm开发时,对于listpicker 木有发现啥问题
当在mvvm中 listpicker 的SelectionChanged事件,会出现第一次选择某一项时,不管选择哪一项,都会获取到 索引为0的 第一项,第二次选择时,则获取到 第一次选择的那一项,之后以此类推。困惑啊
如果是 直接在cs 文件写该事件的话 在加载的时候 就会运行到 SelectionChanged事件,并获取到默认第一项的值
请教下大家 有没有遇到此类问题
再次感谢
贴上代码
<phone:PhoneApplicationPage.Resources>
<Models:CommonConverters x:Key="CommonConverters"/>
<DataTemplate x:Key="ListPickerFullTemplate">
<TextBlock Text="{Binding Description}" Margin="0,0,0,12" TextWrapping="Wrap"/>
</DataTemplate>
<DataTemplate x:Key="ListPickerItemTemplate">
<TextBlock Margin="0,0,0,0" HorizontalAlignment="Center" Text="{Binding Description}"/>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<toolkit:ListPicker x:Name="_1StepListPicker" Grid.Row="0" ItemsSource="{Binding Products}" FullModeItemTemplate="{StaticResource ListPickerFullTemplate}" ItemTemplate="{StaticResource ListPickerItemTemplate}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<Command:EventToCommand Command="{Binding SelectedProductCommand}" CommandParameter="{Binding SelectedItem,ElementName=_1StepListPicker}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</toolkit:ListPicker>
--------------------------------------------------------------
SelectedProductCommand = new RelayCommand<Product>((p) =>
{
if (Products.CurrentItem != null)
{
//获取当前选中列
Product selectedProduct = (Product)p;// Products.CurrentItem;
}
}
);
public ObservableCollection<Product> Items
{
get;
private set;
}
public RelayCommand<Product> SelectedProductCommand
{
get;
private set;
}
private ICollectionView CreateView(IEnumerable source)
{
CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = source;
return cvs.View;
}
private ICollectionView products;
public ICollectionView Products
{
get
{
if (this.products == null)
{
this.products = CreateView(this.Items);
}
return this.products;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Name> _listOfnames;
public ObservableCollection<Name> ListOfNames
{
get { return _listOfnames; }
set
{
_listOfnames = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("ListOfNames"));
}
}