Link to home
Start Free TrialLog in
Avatar of Burzhuin
Burzhuin

asked on

I need to clear List Box in MVVM panel.

I need to clear List Box in MVVM Panel to load different values. But there is no old method Clear, e.g. lstMine.Clear. I am new to MVVM and have no idea how to get around it.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Burzhuin;

If memory serves me correctly you should be able to do this.
 lstMine.Items.Clear()

Open in new window

Avatar of Burzhuin
Burzhuin

ASKER

It is not working in MVVM panel.
Can you post the XAML and the code behind, thanks.
I am getting this exception:

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead
Here is XAML code:

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Nwh.Member.Utility.ExceptionTracker.Views" x:Class="SearchPanelView"
             xmlns:local1="clr-namespace:Nwh.Member.Utility.ExceptionTracker.Views"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d"
             d:DesignHeight="900" d:DesignWidth="1200">



    <UserControl.Resources>
        <local1:DatePickerConverter  x:Key="shortDateConverter" />
        <!--<Style TargetType="DatePickerTextBox">
            <Setter Property="Background" Value="AliceBlue" />
        </Style>-->
    </UserControl.Resources>
    <!--ScrollViewer HorizontalScrollBarVisibility="Visible"  VerticalScrollBarVisibility="Visible">-->

    <Grid>
        <DockPanel  LastChildFill="false" Margin="0,0,0,-36" >
            <Expander ExpandDirection="Down" IsExpanded="True" DockPanel.Dock="Top"  >
                <!--Search Top Partion Below-->
                <DockPanel DockPanel.Dock="Top"  Margin="10, 0, 10, 0" LastChildFill="False" HorizontalAlignment="Stretch"  >

                    <DockPanel DockPanel.Dock="Left" Margin="0,0,0,0" LastChildFill="False" HorizontalAlignment="Stretch"  >
                        <DockPanel   DockPanel.Dock="Bottom"   HorizontalAlignment="Stretch"  RenderTransformOrigin="0.5,0.5" VerticalAlignment="Stretch"  MinHeight="150"  >
                            <StackPanel Orientation="Vertical" >
                                <Label Content="Search Citeria" FontSize="14" FontWeight="Bold" Width="678" />
                                <TextBox Margin="10, 2, 0, 10" MinHeight="120" Text="{Binding SearchCriteria}" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />
                                <!--<TextBox Margin=" 10, 5, 0, 10" Text="{Binding SearchResult}"></TextBox>-->
                            </StackPanel>
                        </DockPanel>
                        <!--<Border BorderBrush="DarkBlue" BorderThickness="1" CornerRadius="10" >-->
                        <StackPanel DockPanel.Dock="Left" Orientation="Vertical" Margin="10,0,0,0"  HorizontalAlignment="Stretch" >
                            <TextBlock FontSize="14" FontWeight="Bold">Sources</TextBlock>
                            <RadioButton Content="834" HorizontalAlignment="Left" Margin="10,20,0,0" VerticalAlignment="Top" GroupName="grpType" Background="AliceBlue"
                                             x:Name="opt834" RenderTransformOrigin="0.409,2.231" IsChecked="{Binding Is834Search}" />
                            <RadioButton Content="Xpress Workflow" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Background="AliceBlue"
                                             GroupName="grpType" x:Name="optXPWF" IsChecked="{Binding IsWorkFlowSearch}"/>
                            <CheckBox Content="Include Unworkable" HorizontalAlignment="Left" Margin="10,20,0,0" VerticalAlignment="Top"
                                          RenderTransformOrigin="-5.377,-7.725" IsChecked="{Binding IsUnworkable}"/>


                        </StackPanel>
                        <!--</Border>-->
                        <!--Source and Filters Panels-->
                        <!--<Border BorderBrush="DarkBlue" BorderThickness="1" CornerRadius="10" Margin="0, 5, 0, 5" >-->
                        <DockPanel DockPanel.Dock="Left" Margin="10, 0, 0, 0">
                            <StackPanel DockPanel.Dock="Bottom" Orientation="Vertical"     >
                                <Label Content="Error Message" Margin="20, 0, 0, 0" />
                                <ComboBox Margin="20, 1, 5, 5" Width="550" x:Name="cboErrorMsg" Background="AliceBlue"   VerticalAlignment="Stretch" HorizontalAlignment="Left"
                                      ItemsSource="{Binding ErrorMessageList}" SelectedItem="{Binding SelectedErrorMessage }" SelectedIndex="0"   >
                                    <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding ErrorMessage}" TextWrapping="Wrap" />
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>
                                </ComboBox>
                            </StackPanel>

                            <StackPanel DockPanel.Dock="Left" Orientation="Vertical" Margin="10,0,0,0" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"  >
                                <TextBlock FontSize="14" FontWeight="Bold" Width="197">Filters</TextBlock>
                                <Label Content="Exception Type" HorizontalAlignment="Left" Margin="20,10,0,0" VerticalAlignment="Top" />

                                <!--Exception Type List in dropdown data setting -->
                                <ComboBox HorizontalAlignment="Left" Margin="20,1,0,0" VerticalAlignment="Top"  x:Name="cboException" MinWidth="196" Background="AliceBlue"
                                          ItemsSource="{Binding ExceptionTypeList}" SelectedItem="{Binding SelectedExceptionType}" IsReadOnly="True" >
                                    <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding ExceptionTypeName }" />
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>
                                </ComboBox>
                                <Label Content="Assigned To" HorizontalAlignment="Left" Margin="20,5,0,0" VerticalAlignment="Top" />
                                <ComboBox HorizontalAlignment="Left" Margin="20,5,0,0" VerticalAlignment="Top"  x:Name="cboAssign" MinWidth="194" Background="AliceBlue"
                                       ItemsSource="{Binding SearchableUserList}" SelectedItem="{Binding SelectedUser}"/>

                            </StackPanel>

                            <StackPanel DockPanel.Dock="Left" Orientation="Vertical" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="top" Height="168"  >
                                <Label Content="Status" HorizontalAlignment="Left" Margin="15,10,0,0" VerticalAlignment="Top" />
                                <ListBox HorizontalAlignment="Left" Height="110" Margin="15,2,0,0" VerticalAlignment="Top"  x:Name="lboStatus" MinWidth="120"
                                     ItemsSource="{Binding SearchableStatusList}" SelectionMode="Multiple"  SelectionChanged="lboStatus_SelectionChanged" Background="AliceBlue" >

                                </ListBox>


                            </StackPanel>
                            <Grid DockPanel.Dock="Left" Margin="0,0,0,0" HorizontalAlignment="Stretch"  >
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Label Content="Source" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Grid.Row="0" />

                                <ListBox HorizontalAlignment="Left" MinHeight="130" Margin="10,2,5,2" VerticalAlignment="Top"  x:Name="lboSource" MinWidth="200" Grid.Row="1"
                                     ItemsSource="{Binding DataSourceList}" SelectionMode="Multiple" SelectionChanged="lboSource_SelectionChanged" Loaded="lboSource_Loaded" Background="AliceBlue" >
                                    <ListBox.ItemTemplate >
                                        <DataTemplate>
                                            <!--<Border BorderBrush="DarkBlue" BorderThickness="1" CornerRadius="3" Margin="2, 2, 2, 2" >-->
                                            <TextBlock Text="{Binding SourceName}" HorizontalAlignment="Stretch" />
                                            <!--</Border>-->
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>

                            </Grid>
                        </DockPanel>
                        <!--</Border>-->


                    </DockPanel>
                    <!--<Border BorderBrush="DarkBlue" BorderThickness="1" CornerRadius="10" Margin="0, 5, 0, 5" >-->
                    <DockPanel DockPanel.Dock="Left" LastChildFill="False" HorizontalAlignment="Stretch" Margin="50, 10, 0, 0"  >
                        <!--<Grid>
                            <Grid.RowDefinitions >
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>-->

                        <Grid Grid.Row="0" MinWidth="380">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <!--<Grid.ColumnDefinitions>
                                <ColumnDefinition Width="150" />
                                <ColumnDefinition Width="0"/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>-->

                            <TextBlock Margin="10, 0, 0, 0" FontSize="14" FontWeight="Black"  Grid.Row="0" HorizontalAlignment="Stretch">Search Value</TextBlock>
                            <!--<StackPanel DockPanel.Dock="Top" Orientation="Horizontal"   >-->
                            <Label Content="Family ID" HorizontalAlignment="Stretch"  Margin="0,10,0,0"   Grid.Row="1" />
                            <TextBox   Height="23"   TextWrapping="Wrap"  VerticalAlignment="Top" MinWidth="225" x:Name="txtFamily"
                                   Margin="70, 10, 0, 0" HorizontalAlignment="Left"  Background="AliceBlue" Text="{Binding FamilyID}" Grid.Row="1" Width="310"   />
                            <!--</StackPanel>-->
                            <!--<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" >-->
                            <Label Content="MMIS ID" HorizontalAlignment="Stretch"  Margin="0,10,0,0" VerticalAlignment="Top"  Grid.Row="2" Height="26" />
                            <TextBox HorizontalAlignment="Stretch"  Height="23" Margin="70,10,0,0" TextWrapping="Wrap" x:Name="txtMMISID" VerticalAlignment="Top"  Grid.Row="2"
                                     MinWidth="225" Background="AliceBlue" Text="{Binding MMISID}" />
                            <!--</StackPanel>-->
                            <!--<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" >-->
                            <Label Content="SSN" HorizontalAlignment="Stretch" Margin="0,10,0,0" VerticalAlignment="Top"  Grid.Row="3" Height="26" />
                            <TextBox HorizontalAlignment="stretch" Height="23" Margin="70,10,0,0" TextWrapping="Wrap" x:Name="txtSSN" VerticalAlignment="Top" Grid.Row="3"
                                     MinWidth="225" Background="AliceBlue" Text="{Binding SSN}" />
                            <!--</StackPanel>-->
                            <!--<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" >-->
                            <Label Content="Last Name" HorizontalAlignment="Stretch"  Margin="0,10,0,0" VerticalAlignment="Top" Grid.Row="4" Height="26" />
                            <TextBox HorizontalAlignment="Stretch"  Height="23" Margin="70,10,0,0" TextWrapping="Wrap" x:Name="txtLastName" VerticalAlignment="Top" Grid.Row="4"
                                     MinWidth="225" Background="AliceBlue" Text="{Binding LastName}" />

                            <!--</StackPanel>-->
                            <!--<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" >-->
                            <Label Content="Date Worked" HorizontalAlignment="Stretch"  Margin="0,10,0,0" VerticalAlignment="Top"  Grid.Row="5" Height="26"/>
                            <!--<TextBox HorizontalAlignment="Left" Height="23" Margin="5,10,0,0" TextWrapping="Wrap" x:Name="txtDateWork" VerticalAlignment="Top" Grid.Row="5" Grid.Column="2"
                                     MinWidth="125" Background="AliceBlue" Text="{Binding DateWork, Converter={StaticResource shortDateConverter }}" Width="125" />-->
                            <DatePicker HorizontalAlignment="Stretch" Height="23" Margin="82,12,0,2" x:Name="txtDateWork" Grid.Row="5"  Background="AliceBlue"
                                        SelectedDate ="{Binding DateWork, Converter={StaticResource shortDateConverter}}" DisplayDate="{Binding WorkDisplayDate}"/>
                            <!--</StackPanel>-->
                            <!--<StackPanel DockPanel.Dock="Top">-->
                            <Label Content="Workflow ID" HorizontalAlignment="Stretch" Margin="0, 10, 0, 0" VerticalAlignment="Top" Grid.Row="6" Height="25" />
                            <TextBox Margin="82,10,0,0" x:Name="txtWID" Grid.Row="6" Background="AliceBlue" Text="{Binding WorkflowID}" />
                            <TextBlock HorizontalAlignment="Center" FontSize="14" FontWeight="Black" Grid.Row="7"  Margin="46,0,20,0" >Process Date</TextBlock>


                            <!--</StackPanel>-->
                            <!--<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" >-->
                            <Label Content="From" HorizontalAlignment="Stretch"  Margin="10,2,0,0" VerticalAlignment="Top" Grid.Row="8" Grid.Column="0" Height="26"  />
                            <!--<TextBox HorizontalAlignment="Left" Height="23" Margin="45,5,0,0" TextWrapping="Wrap" x:Name="txtFromDate" VerticalAlignment="Top" Grid.Row="7"
                                     MinWidth="100" Background="AliceBlue" Text="{Binding FromDate}" Width="100"/>
                            <Button HorizontalAlignment="Left" Margin="0,5" Content="..." x:Name="btnCalendar1" Width="25"  Grid.Row="7" Grid.Column="2"/>-->
                            <DatePicker HorizontalAlignment="Stretch" Height="23" Grid.Row="8"  SelectedDate="{Binding FromDate, Converter={StaticResource shortDateConverter }}"  
                                  DisplayDate="{Binding ProcFromDisplayDate}"      Margin="60,2, 200,0" Background="AliceBlue"   />

                            <Label Content="To" HorizontalAlignment="Left" Margin="180,5,0,0" VerticalAlignment="Top" Grid.Row="8"  Height="26"  />
                            <!--<TextBox HorizontalAlignment="Left" Height="23" Margin="90,5,0,0" TextWrapping="Wrap" x:Name="txtToDate" VerticalAlignment="Top" Grid.Row="7" Grid.Column="2"
                                     MinWidth="100" Background="AliceBlue" Text="{Binding ToDate}" Width="100"/>
                            <Button HorizontalAlignment="Left" Margin="195,5,0,5" x:Name="btnCalendar2" Width="25" Content="...."  Grid.Row="7" Grid.Column="2"/>-->
                            <DatePicker HorizontalAlignment="Stretch" Height="23" Margin="221,6,0,1" DisplayDate="{Binding ProcToDisplayDate}"
                                        SelectedDate ="{Binding ToDate, Converter={StaticResource shortDateConverter }}" Grid.Row="8"  />
                            <Label Content="Environment" HorizontalAlignment="Stretch"  Margin="0,0,350,0" x:Name="lblEnv" VerticalAlignment="Top" Grid.Row="9" Grid.Column="0" Height="26" />
                            <!--</StackPanel>-->
                            <!--<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" >-->
                            <Button  Content="Search" Margin="60,10,200,10" x:Name="btnSearch" Click="btnSearch_Click" Grid.Row="9" />
                            <!--="{Binding CmdSearch}" /> Command="{Binding ResetAction}" -->
                            <Button Content="Reset" Margin="221,10,53,10" x:Name="btnReset" Click="btnReset_Click"  Grid.Row="9" />
                            <Button  Content="Show Env" Margin="100,60,100,-12" x:Name="btnShowEnv" Click="btnShowEnv_Click" Grid.Row="9" />
                            <!--</StackPanel>-->
                        </Grid>
                        <!--</Grid>-->
                    </DockPanel>
                    <!--</Border>-->
                    <Popup  x:Name="popUp1" IsOpen="{Binding IsBusy}" Placement="Bottom" AllowsTransparency="True"  PopupAnimation="Slide"  VerticalOffset="-30" HorizontalOffset="70" >
                        <!--<Grid>-->
                        <!--<StackPanel Height="150" Width="200" Background="Yellow" >-->
                        <TextBlock Text="Searching Exception Trackers..." Background="Yellow"  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16" Margin=" 800, 0, 0, 0" Width="240"   />
                        <!--</StackPanel>-->
                        <!--<ProgressBar HorizontalAlignment="Left"  
                     Name="progressBar1" Height="25"
                     VerticalAlignment="Center" Width="130"
                     Foreground="#FF3EA3EA"
                     Value="{Binding ElementName=textBox1, Path=Text.Length, Mode=OneTime}"
                     Maximum="140" ForceCursor="False" />-->


                        <!--</Grid>-->
                    </Popup>
                </DockPanel>
            </Expander>
            <!--
            <DockPanel DockPanel.Dock="Top"  LastChildFill="False" Margin="0, 0, 0, 0" VerticalAlignment="Stretch" >
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Background="NavajoWhite"  Height="40"  HorizontalAlignment="Stretch"  >
                    <TextBlock Margin="10, 10, 0, 0">Edit all entries in column</TextBlock>
                    <CheckBox  Margin="60, 10, 0, 0" x:Name="chkBatchStatus" />
                    <ComboBox Margin="5,5,0,10" MinWidth="150" x:Name="cboBatchStatus"  />
                    <CheckBox  Margin="60, 10, 0, 0" x:Name="chkBatchUser" />
                    <ComboBox Margin="5,5,0,10" MinWidth="150" x:Name="cboBatchUser" />
                    <Button Content="Batch Update" x:Name="btnBatchUpdate" Margin="360,5,0,10" Width="99" Click="btnBatchUpdate_Click"  />
                </StackPanel>




            </DockPanel>
            -->
            <!--<StackPanel DockPanel.Dock="Bottom" Background="NavajoWhite"  Height="45" Orientation="Horizontal" HorizontalAlignment="Stretch"  >

                <Button Margin="80, 5, 0, 0" Content="Detail" x:Name="btnDetail" Width="100" Height="25"/>
                <Button Margin="80, 5, 0, 0" Content="History" x:Name="btnHistory" Width="100" Height="25"/>
                <Button Margin="80, 5, 0, 0" Content="Export" x:Name="btnExport" Width="100" Height="25"/>
                <Button Margin="80, 5, 0, 0" Content="Save" x:Name="btnSave" Width="100" Height="25"/>
                <Button Margin="80, 5, 0, 0" Content="Cancel" x:Name="btnCancel" Width="100" Height="25"/>
            </StackPanel>-->
            <Border MinHeight="350" MinWidth="1190" Margin="5,0" BorderBrush="DarkBlue" BorderThickness="3" CornerRadius="10" Height="471" VerticalAlignment="Top">

                <DockPanel DockPanel.Dock="Top"  x:Name="pnl834Result" Visibility="Visible"   Margin="0,0,0,83" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"   >

                    <!--<local:EDI834SearchResults  VerticalAlignment="Top" x:Name="grid834Result"/>-->
                </DockPanel>
            </Border>
            <!--<DockPanel DockPanel.Dock="Top" x:Name="pnlWFResult" Visibility="Hidden"  Margin="0, 0, 0, 0" >
                <local:XPWorkflowResultsView  x:Name="gridWFResult" />
            </DockPanel>-->


        </DockPanel>

        <!--
        <DockPanel Margin="0,35,0,0" LastChildFill="False" VerticalAlignment="Top" HorizontalAlignment="Left" Height=" 30" >
           
        </DockPanel>-->
        <!--<DockPanel Margin="770, 0, 0, 0" LastChildFill="False" VerticalAlignment="Top" HorizontalAlignment="Left" Height=" 30" >
            </DockPanel>-->
        <!--
       
           
            <TextBlock HorizontalAlignment="Left" Margin="26,245,0,0" TextWrapping="Wrap"  
                  x:Name="txtSeaarchCondition" VerticalAlignment="Top" Height="129" Width="721" Background="AntiqueWhite"/>
            <Button Content="Searh" HorizontalAlignment="Left" Margin="837,277,0,0" VerticalAlignment="Top" MinWidth="105" x:Name="btnSearch"/>
         -->
    </Grid>

</UserControl>
Here is the code behind it:

Imports Nwh.Member.Utility.ExceptionTracker.Models
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class SearchPanelView

    Private stOutput As String


    Private Sub btnSearch_Click(sender As Object, e As Windows.RoutedEventArgs)

        'Me.Height = 300
        'Me.Width = 300

        Try
            Dim bgWorker As New BackgroundWorker
            AddHandler bgWorker.DoWork, AddressOf DoSearch
            AddHandler bgWorker.RunWorkerCompleted, AddressOf DoSearchComplete
            popUp1.IsOpen = True
            Dim searchVM As SearchPanelVM = Me.DataContext
            stOutput = String.Empty
            bgWorker.RunWorkerAsync(searchVM)
        Catch ex As Exception
            Utility.delWriteToErrorLog("SearchPanelView.btnSeach_Click", ex)
            System.Windows.MessageBox.Show(ex.Message)
        End Try


    End Sub


    ''' <summary>
    ''' Run the search in background worker
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DoSearch(sender As Object, e As DoWorkEventArgs)
        Try
            Dim searchVM As SearchPanelVM = e.Argument

            Dim resultVM As SearchResultsVM = searchVM.GetSearchResult(stOutput)

            e.Result = resultVM
        Catch ex As Exception
            System.Windows.MessageBox.Show(ex.Message)
            Utility.delWriteToErrorLog("SearchPanelView.DoSearch", ex)

        End Try
    End Sub

    ''' <summary>
    ''' Output the search result to frontend after background worker finish the search
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DoSearchComplete(sender As Object, e As RunWorkerCompletedEventArgs)
        Try

            Dim searchVM As SearchPanelVM = Me.DataContext
            If Not String.IsNullOrEmpty(stOutput) Then
                popUp1.IsOpen = False
                System.Windows.MessageBox.Show(stOutput)

                Return
            End If

            Dim resultVM As SearchResultsVM = e.Result
            pnl834Result.Visibility = Windows.Visibility.Visible
            '   Dim gridResult As Windows.UIElement
            pnl834Result.Children.Clear()
            If resultVM.searchResultTrackers.Count = 0 Then
                'stOutput = "No result found"
                popUp1.IsOpen = False
                System.Windows.MessageBox.Show("No result found")

                Return
            End If
            Dim grid834 As New EDI834SearchResults
            If searchVM.Is834Search Then

                grid834.IsBatchChange = True
                grid834.DataContext = resultVM

                pnl834Result.Children.Add(grid834)

            Else
                Dim gridWF As New XPWorkflowResultsView
                gridWF.DataContext = resultVM
                pnl834Result.Children.Add(gridWF)


            End If
            resultVM.TotalResults = String.Format("Total Records: {0}", resultVM.searchResultTrackers.Count.ToString)
            'String.Format("1 - {0} Records of Total {0} Records", resultVM.searchResultTrackers.Count.ToString)
            pnl834Result.DataContext = resultVM

            grid834.IsBatchChange = False
            popUp1.IsOpen = False
        Catch ex As Exception
            Utility.delWriteToErrorLog("SearchPanelView.DoSearchComplete", ex)
            'If popUp1.IsOpen Then
            '  popUp1.IsOpen = False
            'End If
        End Try
    End Sub

    Private Sub lboSource_SelectionChanged(sender As Object, e As Windows.Controls.SelectionChangedEventArgs)
        'Dim lstSources As New List(Of ExceptionTrackerSource)
        'Dim iAllSources As Boolean = False
        '
        '    If lboSource.SelectedItems.Count > 0 Then
        '        For Each itm In lboSource.SelectedItems
        'Dim st As ExceptionTrackerSource = DirectCast(itm, ExceptionTrackerSource)
        '            ExceptionTrackerSource.RIFLAG = frmSearch.isRIET
        '            If st.EqualTo(ExceptionTrackerSource.GetAllSource) Then
        '                iAllSources = True
        '                lstSources = New List(Of ExceptionTrackerSource)()
        '                lstSources.Add(st)
        '                Exit For
        '            End If
        '            lstSources.Add(st)
        '        Next
        '        End If
        '    If iAllSources And lboSource.SelectedItems.Count > 1 Then
        '        lboSource.UnselectAll()
        '        lboSource.SelectedIndex = 0
        '    Else
        '        lstSources = New List(Of ExceptionTrackerSource)()
        '        For Each itm1 In lboSource.SelectedItems
        'Dim st1 As ExceptionTrackerSource = DirectCast(itm1, ExceptionTrackerSource)
        '            lstSources.Add(st1)
        '        Next
        '    End If
        'Dim spVM As SearchPanelVM = Me.DataContext
        '    spVM.SelectedFileSources = lstSources
    End Sub

    Private Sub lboStatus_SelectionChanged(sender As Object, e As Windows.Controls.SelectionChangedEventArgs)
        ' System.Windows.MessageBox.Show(sender.GetType.ToString())
        Dim lstStatus As New List(Of ExceptionTrackerStatus)
        Dim iAllStatus As Boolean = False
        ' Dim totalSelectedStatus = lboSource.SelectedItems.Count
        For Each itm In lboStatus.SelectedItems
            Dim stu As ExceptionTrackerStatus = DirectCast(itm, ExceptionTrackerStatus)
            If stu.EqualTo(ExceptionTrackerStatus.AllStatusStatus) Then
                iAllStatus = True
                lstStatus = New List(Of ExceptionTrackerStatus)
                lstStatus.Add(stu)
                Exit For
            End If
            'lstStatus.Add(stu)
        Next

        If iAllStatus And lboStatus.SelectedItems.Count > 1 Then
            lboStatus.UnselectAll()

            lboStatus.SelectedIndex = 0
            'lboStatus.SelectedValue = ExceptionTrackerStatus.AllStatusStatus

            '    lboStatus.SelectedItem = lstStatus(0)
        Else
            lstStatus = New List(Of ExceptionTrackerStatus)()
            For Each itm2 In lboStatus.SelectedItems
                Dim stu2 As ExceptionTrackerStatus = DirectCast(itm2, ExceptionTrackerStatus)
                lstStatus.Add(stu2)
            Next
        End If

        Dim spVM As SearchPanelVM = Me.DataContext
        spVM.SelectedStatuses = lstStatus

    End Sub

    'Private Sub btnBatchUpdate_Click(sender As Object, e As Windows.RoutedEventArgs)
    '    If pnl834Result.Children.Count = 0 Then
    '        Return
    '    End If
    '    Dim resultVM As SearchResultsVM = pnl834Result.DataContext
    '    System.Windows.MessageBox.Show(resultVM.SelectedTrackerVMs.Count.ToString())
    '    'Dim obj As Object = pnl834Result.Children(0)


    'End Sub


    Private Sub btnLayout_Click(sender As Object, e As Windows.RoutedEventArgs)
        'Me.Height = 300
        'Me.Width = 300
        'Dim md As New SearchPanelView
        'md.Height = 300
        'md.Width = 300




    End Sub

    Private Sub btnReset_Click(sender As Object, e As Windows.RoutedEventArgs)
        Dim iResult = System.Windows.MessageBox.Show("Do you want to clear all search conditions?", "Clear Search Result", vbYesNo)
        If iResult = Windows.MessageBoxResult.Yes Then
            Dim spVM As SearchPanelVM = Me.DataContext
            spVM.DoReset()
            lboStatus.UnselectAll()
            lboSource.UnselectAll()
            pnl834Result.Children.Clear()

        End If


    End Sub
    Private Sub btnShowEnv_Click(sender As Object, e As Windows.RoutedEventArgs)

        If frmSearch.isRIET = True Then
            Dim cnt As Integer
            MsgBox("Hello from Donald!")
            ExceptionTrackerSource.RIFLAG = True
            'Dim spVM As SearchPanelVM = Me.DataContext
            'Dim lstSources As New List(Of ExceptionTrackerSource)

            'For Each Item In lstSources
            'lboSource.ClearValue(lboSource.Items.Clear) '= ExceptionTrackerSource.ClearAllSource
            'Next
            'spVM.DoReset()
            'lboSource.DataContext = Me.DataContext
            cnt = lboSource.Items.Count
            MsgBox("Over all there are " & cnt & " items in lboSource ListBox")
            For i = 0 To lboSource.Items.Count - 1
                'lboSource.ItemsSource.GetType()
                lboSource.Items.RemoveAt(i)
            Next
            'lstSources = New List(Of ExceptionTrackerSource)
            'MsgBox(lstSources.Count)
            'lstSources.Clear()
            'MsgBox(lstSources.Count)
            'lblEnv.Content = "True!"
        Else
            lblEnv.Content = "False!"
        End If
    End Sub

    Private Sub lboSource_Loaded(sender As Object, e As Windows.RoutedEventArgs) Handles lboSource.Loaded
        'If frmSearch.isRIET = True Then
        Dim lstSources As New List(Of ExceptionTrackerSource)
        'Dim st As New ExceptionTrackerSource = ExceptionTrackerSource
        'Dim st As ExceptionTrackerSource = DirectCast(item, ExceptionTrackerSource)
        'lboSource.Items.Clear()
        ExceptionTrackerSource.RIFLAG = frmSearch.isRIET
        lstSources.Clear()
        'Dim iAllSources As Boolean = False
        'ExceptionTrackerSource.RIFLAG = frmSearch.isRIET
        'Dim st As New ExceptionTrackerSource = ExceptionTrackerSource
        'Dim st As ExceptionTrackerSource = DirectCast(itm, ExceptionTrackerSource)
        'ExceptionTrackerSource.RIFLAG = frmSearch.isRIET
        'If st.EqualTo(ExceptionTrackerSource.GetAllSource) Then
        'iAllSources = True
        lstSources = New List(Of ExceptionTrackerSource)
        'lstSources.Clear()
        'lstSources.Add(st)

        'End If
        'MsgBox("The value of ExceptationTrackerSource property is " & ExceptionTrackerSource.RIFLAG & "!")
        'End If
        Dim spVM As SearchPanelVM = Me.DataContext
        spVM.SelectedFileSources = lstSources
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class
OK, First when posting code snippets use the CODE icon on the toolbox. First paste your code to the textbox and then highlight the code and then click the CODE icon on the toolbox. It helps those who are trying to help you.

Second I did a searched on the code and could not find a ListBox called lstMine. Please state what parts of the code you are having issues with.
I have problem with lboSource. What I am trying to do is to clear it from previously loaded data and reload it:

Public Shared Function GetExceptionSourceList()
        Dim lst As List(Of NWH_Exception_DataSource) = ExceptionTrackerModel.getFileSourcesFromDB
        Dim dsList As List(Of ExceptionTrackerSource)
        'Dim flg As New GlobalVariables

        'MsgBox("Did I pass global variable value? Here is the anser: " & GlobalVariables.MyGlobalFlag.ToString)

        If ExceptionTrackerSource.RIFLAG = True Then
            dsList = (From l In lst Where l.SourceID = 11
                        Select New ExceptionTrackerSource With {
                                    .SourceID = l.SourceID,
            .SourceName = l.Source
                                    }).OrderBy(Function(f) f.SourceName).ToList()
        Else
            'MsgBox("Something went wrong!")
            dsList = (From l In lst Where l.SourceID <> 11
                       Select New ExceptionTrackerSource With {
                                    .SourceID = l.SourceID,
            .SourceName = l.Source
                                   }).OrderBy(Function(f) f.SourceName).ToList()
            dsList.Insert(0, ExceptionTrackerSource.GetAllSource)
        End If
        Return dsList
    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, Fernando! I was able to clear List Box.  I used this Function:
 
   Public Shared Function ClearAllSource() As ExceptionTrackerSource
        Dim lst As List(Of NWH_Exception_DataSource) = ExceptionTrackerModel.getFileSourcesFromDB
        Dim dsList As List(Of ExceptionTrackerSource) = ExceptionTrackerSource.GetExceptionSourceList
        dsList.Clear()
    End Function

Open in new window

And I need only one line to call this Function:
lboSource.DataContext = ExceptionTrackerSource.ClearAllSource

Open in new window

However when I tried to repopulate the list box using this:
 
lboSource.DataContext = ExceptionTrackerSource.GetExceptionSourceList

Open in new window

My list box remained empty. I know I am doing something wrong. But what? Would you be so kind to tell me?
Try it this way
lboSource.DataContext = Nothing
lboSource.DataContext = ExceptionTrackerSource.GetExceptionSourceList

Open in new window

Still does not work.
But this works for some reason:
lboSource.DataContext = ExceptionTrackerSource.ClearAllSource
lboSource.ItemsSource = ExceptionTrackerSource.GetExceptionSourceList
lboSource.DataContext = ExceptionTrackerSource.GetExceptionSourceList

Open in new window

Thank you Fernando for pointing me to the right direction. I do appreciate your help.
Not a problem Burzhuin, glad you got it working.👍