Answered by:
SelectedItems in Listview inserted twice

Question
-
Hi all,
I've a ListView in my App which has an ItemsSource of an List. No I try to insert items of the ListView in the SelectedItems object. What happens is quite weird to me.
When I first navigate to the page, bind the data to the ListView and set the SelectedItems, the number of items in SelectedItems are correct. When I hit a button to load something else, the number of items in SelectedItems is doubled. Every item is in SelectedItems twice. When I bind the data to the ListView again, insert the SelectedItems and hit the button again, everything is fine. i have no idea what's wrong.
This is my ListView:
<ListView Foreground="{StaticResource MPGETextColor}" SelectionMode="Multiple" x:Name="LVStatChoices" Margin="-5,10,0,0"
ItemsSource="{Binding}" > <ListView.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding lblName}"></TextBlock> <TextBlock Text="{Binding lblKey}" Visibility="Collapsed"></TextBlock> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>OnNavigatedTo I bind the data to the ListView:
LVStatChoices.ItemsSource = names;
and then I insert the selected items like this:
LVStatChoices.SelectedItems.Clear(); foreach (lblName ln in LVStatChoices.Items) { if (ln.selectDefault == true && !LVStatChoices.SelectedItems.Contains(ln)) { LVStatChoices.SelectedItems.Add(ln); } }
Currently 8 Items are selected in the UI because 8 items has selectedDefault == true. If I stop debugging after the foreach loop there are 8 items in SelectedItems which is fine. As mentioned above, when I click on a button and stop at
if (LVStatChoices.SelectedItems.Count == 0)
there are 16 Items in SelectedItems.
Again this behaviour occured after migrating my app from Windows 8 to 8.1.
Does anybody has an idea what I'm doing wrong?
Friday, March 28, 2014 2:53 PM
Answers
-
Hi,
I test your project. Your codes seems correct. That's may be a bug in windows store app and I suggest you can edit your code like below:
In XAML:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top"> <local:MyListView SelectionMode="Multiple" x:Name="LVStatChoices" Width="200" Height="400" Margin="10" > <local:MyListView.ItemContainerStyle > <Style TargetType="ListViewItem"> <Setter Property="IsSelected" Value="{Binding selectDefault, Mode=TwoWay}"/> </Style> </local:MyListView.ItemContainerStyle> <local:MyListView.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding lblName}"></TextBlock> <TextBlock Text="{Binding lblKey}" Visibility="Visible"></TextBlock> </StackPanel> </DataTemplate> </local:MyListView.ItemTemplate> </local:MyListView> <Button x:Name="BTNTest" Content="Test" Margin="10" HorizontalAlignment="Left" Tapped="BTNTest_Tapped"></Button> </StackPanel> </Grid>
code-behind:
public sealed partial class MainPage : Page { public List<statLabelName> stlList { get; set; } public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { stlList = new List<statLabelName>(); stlList.Add(new statLabelName { lblKey = "Key1", lblName = "Label 1", selectDefault = false }); stlList.Add(new statLabelName { lblKey = "Key2", lblName = "Label 2", selectDefault = true }); stlList.Add(new statLabelName { lblKey = "Key3", lblName = "Label 3", selectDefault = false }); stlList.Add(new statLabelName { lblKey = "Key4", lblName = "Label 4", selectDefault = true }); stlList.Add(new statLabelName { lblKey = "Key5", lblName = "Label 5", selectDefault = true }); LVStatChoices.ItemsSource = stlList; } private void BTNTest_Tapped(object sender, TappedRoutedEventArgs e) { int selItems = LVStatChoices.SelectedItems.Count; } } public struct statLabelName { public string lblName { get; set; } public string lblKey { get; set; } public bool selectDefault { get; set; } } public class MyListView : ListView { protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item) { base.PrepareContainerForItemOverride(element, item); // ... ListViewItem listItem = element as ListViewItem; Binding binding = new Binding(); binding.Mode = BindingMode.TwoWay; binding.Source = item; binding.Path = new PropertyPath("selectDefault"); listItem.SetBinding(ListViewItem.IsSelectedProperty, binding); } }
I define a MyListView which inherit from ListView class and set the ListviewItem IsSelected property to selectDefault.
Best Wishes!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Marked as answer by jev01 Wednesday, April 9, 2014 7:57 AM
Tuesday, April 8, 2014 8:40 AM
All replies
-
Hi,
There is not enough information to detect why your problem occurs. Please upload your project all share a simple project into skyDrive so that we can test it.
Best wishes!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.Monday, March 31, 2014 4:23 AM -
Thanks for your reply.
Please download a sample project here.
If you stop at line 47 in MainPage.xaml.cs, you'll see that LVStatChoices.SelectedItems contains 3 objects. If you click on the button an stop at line 52, LVStatChoices.SelectedItems will contain 6 items. It contains every statLabelName item, which has selectDefault = true, twice.
Thanks for your help,
jev
Friday, April 4, 2014 9:44 AM -
Hi,
I test your project. Your codes seems correct. That's may be a bug in windows store app and I suggest you can edit your code like below:
In XAML:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top"> <local:MyListView SelectionMode="Multiple" x:Name="LVStatChoices" Width="200" Height="400" Margin="10" > <local:MyListView.ItemContainerStyle > <Style TargetType="ListViewItem"> <Setter Property="IsSelected" Value="{Binding selectDefault, Mode=TwoWay}"/> </Style> </local:MyListView.ItemContainerStyle> <local:MyListView.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding lblName}"></TextBlock> <TextBlock Text="{Binding lblKey}" Visibility="Visible"></TextBlock> </StackPanel> </DataTemplate> </local:MyListView.ItemTemplate> </local:MyListView> <Button x:Name="BTNTest" Content="Test" Margin="10" HorizontalAlignment="Left" Tapped="BTNTest_Tapped"></Button> </StackPanel> </Grid>
code-behind:
public sealed partial class MainPage : Page { public List<statLabelName> stlList { get; set; } public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { stlList = new List<statLabelName>(); stlList.Add(new statLabelName { lblKey = "Key1", lblName = "Label 1", selectDefault = false }); stlList.Add(new statLabelName { lblKey = "Key2", lblName = "Label 2", selectDefault = true }); stlList.Add(new statLabelName { lblKey = "Key3", lblName = "Label 3", selectDefault = false }); stlList.Add(new statLabelName { lblKey = "Key4", lblName = "Label 4", selectDefault = true }); stlList.Add(new statLabelName { lblKey = "Key5", lblName = "Label 5", selectDefault = true }); LVStatChoices.ItemsSource = stlList; } private void BTNTest_Tapped(object sender, TappedRoutedEventArgs e) { int selItems = LVStatChoices.SelectedItems.Count; } } public struct statLabelName { public string lblName { get; set; } public string lblKey { get; set; } public bool selectDefault { get; set; } } public class MyListView : ListView { protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item) { base.PrepareContainerForItemOverride(element, item); // ... ListViewItem listItem = element as ListViewItem; Binding binding = new Binding(); binding.Mode = BindingMode.TwoWay; binding.Source = item; binding.Path = new PropertyPath("selectDefault"); listItem.SetBinding(ListViewItem.IsSelectedProperty, binding); } }
I define a MyListView which inherit from ListView class and set the ListviewItem IsSelected property to selectDefault.
Best Wishes!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Marked as answer by jev01 Wednesday, April 9, 2014 7:57 AM
Tuesday, April 8, 2014 8:40 AM -
Thanks Anne, it works like a charm!
After days of trying I've a solution.
Wednesday, April 9, 2014 7:57 AM