How to save the checks from a checkbox to the database?

0

I developed an application in WPF with C #. I have a window that contains a Combobox where the Items are composed of Checkbox with a foreach result of a table. My question is: How do I keep in the database with LINQ, the checkbox check that are inside the Combobox?

-XAML <ComboBox x:Name="Cbx_Programas">
                        <ComboBoxItem >
                            <CheckBox x:Name="Cbox_Programas"/>
                        </ComboBoxItem>
                    </ComboBox>

-Code-Behind foreach (var c in dbContext.Software)
        {
            Cbox_Programas = new System.Windows.Controls.CheckBox();
            Cbox_Programas.Content = c.Nombre;
            Cbx_Programas.Items.Add(Cbox_Programas);
        }

    
asked by Gabriel Mendez 12.12.2018 в 18:55
source

1 answer

1

You should not access the checkbox control to have the selection, you should binde the data and access the data to see if the option is selected

Using Checkbox in Combo Box

basically you define

<ComboBox Name="cmb" Margin="5" Height="35" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Margin="5" IsChecked="{Binding IsVisited}"/>
                <TextBlock Margin="5" Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

You will see how the Binding is used to assign the data, you define a class

public class TripInfo
{
    public Boolean IsVisited{ get; set; }

    public String Name{ get; set; }
}

and assign it as datasurce of the combo

List<TripInfo> tripList = new List<TripInfo>();
tripList.Add(new TripInfo() { IsVisited = false, Name= "item1"});
tripList.Add(new TripInfo() {IsVisited = false, Name = "item2"});

cmb.ItemsSource = tripList;

The idea is that you iterate the object list to know which ones are marked.

Note: The names of the classes and properties are for example only, you must adapt it to your case

    
answered by 12.12.2018 в 21:19