I have a ListView with several checkboxes contained that are loaded from a database. For just one of these checkboxes, I'd like to have a messagebox display when it is checked. I will then capture the answer to that question as data.
I would think I need to use a ListView.SelectedItem in the code-behind but this displays the messagebox only when the pertinent checkbox is the only box checked. I can get it so that the messagebox is displayed, but only if that checkbox is the only box checked. Also, the message will continue to display if I leave that box checked and check/uncheck other boxes. I don't want this. I need it to display only at the moment it's checked regardless of how many other boxes are checked.
So I changed it to .SelectedItems but this isn't working either. My current attempt is as follows:
XAML:
<Grid> <ScrollViewer> <StackPanel Orientation="Vertical"><Label Content="STATS" /> <ListView x:Name="lstStats" ItemsSource="{Binding Stats}" MaxWidth="684" MinHeight="25" Margin="0" BorderThickness="0"> <ListView.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal" MaxWidth="650" /> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical" VerticalAlignment="Top"> <StackPanel Orientation="Horizontal" Grid.Column="1" VerticalAlignment="Top"> <CheckBox x:Name="chkBox" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected, Mode=OneWayToSource}" Margin="0,5,2,0" Content="{Binding Name}" /> </StackPanel> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackPanel> </ScrollViewer> </Grid>
Code-Behind
Public Sub IsSelected(sender As Object, e As RoutedEventArgs) Handles lstStats.SelectionChanged Try Dim chkStats = New ObservableCollection(Of StatInfo) For Each item In lstStats.SelectedItems Dim selectItem As StatInfo = CType(item, StatInfo) If selectItem.Description = "Item Description to display MessageBox" Then Dim ask As MsgBoxResult = MsgBox("MessageBox text", MsgBoxStyle.YesNoCancel) If ask = MsgBoxResult.Yes Then sexCrimeStat.Stats.Add(New StatInfo With {.IsSelected = True, .Name = "Foobar", .Id = 25, .Description = "Item Description to display MessageBox", .Code = "Foo"}) gotStat = True ElseIf ask = MsgBoxResult.No Then Exit For ElseIf ask = MsgBoxResult.Cancel Then Exit For End If End If Next Catch ex As Exception MessageBox.Show(ex.Message()) End Try
ViewModel:
Me.Stats = New ObservableCollection(Of StatInfo)() ' Load stats. BindingOperations.EnableCollectionSynchronization(Stats, _statLock) Dim tStats = DirectCast(App.Current, App).Db.GetStatItemsAsync.Result If Not tStats Is Nothing Then For Each item In tStats If item.Active Then Me.Stats.Add(New StatInfo With {.IsSelected = False, .Name = item.Stat, .Id = item.Id, .Description = item.Description, .Code = item.AttributeCode}) End If Next End If
Hello and thank you for contacting Infragistics. Sorry but we don't have a ListBox component, this appears to be a component offered by Microsoft. Here is a similar post I found online that may interest you:
blog.nostatic.org/.../wpf-listview-getting-clicked-item.html