Home > OS >  Issue with Multi Select combo box in WPF ,sometimes value in combo text box shows as System.Data.Dat
Issue with Multi Select combo box in WPF ,sometimes value in combo text box shows as System.Data.Dat

Time:08-31

i have a multi select combo box that has a list of checkboxes and combo box has text property to search the list of items .I can select ,unselect values,search ,clear values correctly and am getting desired output of a multi select combox box .But while testing when i keep selecting combo boxes one after the another ,randomly the text in combox box search changes to System.Data.DataRowView .If i am carefully hitting only the check box then this issue does not happen ,but if i hit the label associated with that checkbox randomly this issue pops up

My code xaml file

  <ComboBox    x:Name="cmbAppNameAddRemoveBranch"  Style="{StaticResource md-2s-combo }"  VerticalAlignment="top" HorizontalAlignment="Left" SelectedValuePath="Content"  SelectionChanged="CmbAppNameAddRemoveBranch_SelectionChanged"     TextBoxBase.TextChanged="CmbAppNameAddRemoveBranch_TextChanged"  IsEditable="True" IsTextSearchEnabled="True" StaysOpenOnEdit="True"   IsTextSearchCaseSensitive="False" Text=""    >
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox Name="chkApp" Width="220" Checked="ChkApp_Checked"  Unchecked="ChkApp_Unchecked"  Content="{Binding APVALDESC}" IsChecked="{Binding Check_Status,Mode=TwoWay}" CommandParameter="{Binding APVALUE}"  >
                                    </CheckBox>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <Button x:Name="Show" Click="Show_Click"> Show selected</Button>
                        <Button x:Name="Clear" Click="Clear_Click">Clear selected</Button>

code behind .cs file

private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            
            string lsErr = "";
            cApplParameter cocAppl = new cApplParameter(Home.psProvider, Home.psconnstr, Home.psAppId1, Home.psSchema, Home.psOldSchema);
            cocAppl.GetApplParameters("VMS3", "APPL", "", ref ldtAllApplName, ref lsErr);
            ldtAllApplName.Columns.Add("Check_Status");

            for(int lirow = 0; lirow <= ldtAllApplName.Rows.Count - 1; lirow  )
            {
                ldtAllApplName.Rows[lirow]["Check_Status"] = "false";
            }

            //cmbAppNameAddRemoveBranch.DisplayMemberPath = "APVALDESC";
            //cmbAppNameAddRemoveBranch.SelectedValuePath = "APVALUE";
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;
        }

        private void CmbAppNameAddRemoveBranch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbAppNameAddRemoveBranch.Text == "System.Data.DataRowView")
            {
                cmbAppNameAddRemoveBranch.Text = "";
                return;
            }
        }

        private void ChkApp_Checked(object sender, RoutedEventArgs e)
        {

            CheckBox clickedBox = (CheckBox)sender;
            
            for(int liRow = 0; liRow <= ldtAllApplName.Rows.Count - 1; liRow  )
            {
                if (clickedBox.Content == ldtAllApplName.Rows[liRow]["APVALDESC"].ToString().Trim())
                {
                    ldtAllApplName.Rows[liRow]["Check_Status"] = true;
                }
            }
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;

        }

        private void ChkApp_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox clickedBox = (CheckBox)sender;

            for (int liRow = 0; liRow <= ldtAllApplName.Rows.Count - 1; liRow  )
            {
                if (clickedBox.Content == ldtAllApplName.Rows[liRow]["APVALDESC"].ToString().Trim())
                {
                    ldtAllApplName.Rows[liRow]["Check_Status"] = false;
                }
            }
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;
        }

        private void Show_Click(object sender, RoutedEventArgs e)
        {
            string lsMessage = "";
            DataTable pdtDetails = new DataTable();
            pdtDetails=ldtAllApplName.Select("Check_Status='true'").CopyToDataTable();
            for(int lirow = 0; lirow <= pdtDetails.Rows.Count - 1; lirow  )
            {
                lsMessage = lsMessage   pdtDetails.Rows[lirow]["APVALDESC"].ToString().Trim()   "-"  pdtDetails.Rows[lirow]["APVALUE"].ToString().Trim()  "\n";
                    
            }
            MessageBox.Show(lsMessage);
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            for (int lirow = 0; lirow <= ldtAllApplName.Rows.Count - 1; lirow  )
            {
                ldtAllApplName.Rows[lirow]["Check_Status"] = "false";
            }
            cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.DefaultView;
            MessageBox.Show("Cleared");
        }

        private void CmbAppNameAddRemoveBranch_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (cmbAppNameAddRemoveBranch.Text == "System.Data.DataRowView")
                {
                    cmbAppNameAddRemoveBranch.Text = "";
                    return;
                }

                DataTable ldtDetail = new DataTable();
                if (ldtAllApplName.Select("APVALDESC LIKE '%"   cmbAppNameAddRemoveBranch.Text.ToString().Trim()   "%'").Count() > 0)
                {
                    
                    
                        cmbAppNameAddRemoveBranch.ItemsSource = ldtAllApplName.Select("APVALDESC LIKE '%"   cmbAppNameAddRemoveBranch.Text.ToString().Trim()   "%'").CopyToDataTable().DefaultView;
                        cmbAppNameAddRemoveBranch.IsDropDownOpen = true;
                
                }
                            
                

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }

i tried getting the Text value and manually set it to blank in SelectionChanged and TextChanged event ,but when i debug cmbAppNameAddRemoveBranch.Text this is already blank and condition never meets

if (cmbAppNameAddRemoveBranch.Text == "System.Data.DataRowView")
            {
                cmbAppNameAddRemoveBranch.Text = "";
                return;
            }

CodePudding user response:

you can toggle the checkbox in the click event of the lable ir you can use routed events to make the checkbox recieve the change instead of the lable

CodePudding user response:

With this code below and change to xaml i was able to solve the issue

private void CmbAppNameAddRemoveBranch_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
    
             try
             {
                 cmbAppNameAddRemoveBranch.SelectedValue = "";
                 cmbAppNameAddRemoveBranch.IsDropDownOpen = true;
    
    
             }
 }

In XAML i had to remove this SelectedValuePath="Content"

However IsDropDownOpen=true is not working in SelectionChanged event ,it works in TextChanged event though

  • Related