dimanche 19 mars 2023

CheckBox with DataTemplate Control

I am using a DataGrid to display tax accounts from the DB and calculate the balances for DataGrid selection. DataGrid is filled with an "ObservableCollectionList". Each tax account has a check box. Clicking on the checkbox updates the grid, row with tax account number is removed or added to the data grid. The CheckBoxes are created in a ListBox with DataTemplate and have "ObservableCollectionList" as ItemSource. CheckBoxes with the tax account number that appear in the DataGrid Selection are displayed. Now I have the problem to collecting other checkboxes in the method that are not sent by the method (object sender, RoutedEventArgs e). Without DataTemplate for CheckBoxes it worked fine: BEFORE:

`void OnCheckBoxClick(object sender, RoutedEventArgs e)
        {
            var checkedaccounts = new HashSet<int>();
            foreach (CheckBox checkBox in ListBoxOptions.Items)
            {
                if (checkBox.IsChecked == true)
                {
                    string kto = checkBox.Name.Replace("k_", "");
                    //MessageBox.Show("CheckBox Name: " + kto);
                    int intkto = Int32.Parse(kto);
                    checkedaccounts.Add(intkto);
                }
            }

            if(JobsCollectionView != null)
                JobsCollectionView.Filter =job => checkedaccounts.Contains((job as SaldenData).StKNrFk);
      `

This doesn‘t work anymore!

Question 1: How can I collect all CheckBoxes into a List and checking if they are Checked or Unchecked? Question 2: How can I remove or add them all at once from the JobsCollectionView (DataGrid)? My syntax now:


`

public ObservableCollection\<SaldenData\> SaldenListe { get; set; }
public ObservableCollection\<StKonten\> StKontoData { get; set; }

\

`\<ListBox Name="ListBoxOptions" ItemsSource="{Binding StKontoData}" Background="Transparent" 
                 BorderThickness="0" Grid.Row="1" SelectionMode="Multiple" \>
\<ListBox.ItemTemplate\>
\<DataTemplate\>
\<CheckBox  Content="{Binding StKNummer}" IsChecked="True"
FontSize="10" Height="20" Margin="20,0,0,0" Foreground="Beige"
UseLayoutRounding="True"  Checked="cb_CheckedTrue" Unchecked="cb_CheckedFalse"

                      VerticalAlignment="Bottom" BorderThickness="1,1,1,1" HorizontalAlignment="Left" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>`

Method:

private void cb_CheckedTrue(object sender, RoutedEventArgs e)
{
string st = tb_StNr.ToString();
CheckBox tb = (CheckBox)sender;
MessageBox.Show("Line 168 OnCheckBoxClick: "+ st + " " + tb.Content + " = " + tb.IsChecked);
var checkedaccounts = new HashSet\<int\>();
if (tb.IsChecked == true)
{
            string fkto = tb.Content.ToString();
            MessageBox.Show("Line 174 CheckBox Content: " + fkto);
            int intkto = Int32.Parse(fkto);
            checkedaccounts.Add(intkto);
        }

        if (JobsCollectionView != null)
            JobsCollectionView.Filter = job => checkedaccounts.Contains((job as SaldenData).StKNrFk);

    }



Aucun commentaire:

Enregistrer un commentaire