A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
You could try to refer to the example below.
Table Code :
CREATE TABLE [dbo].[Table]
(
[Id] INT NOT NULL PRIMARY KEY ,
[Name] NCHAR(10) NULL
)
MainWindow.xaml:
<StackPanel>
<ListBox Name="list" Height="300" Margin="4,4,4,4" ItemsSource="{Binding Items}" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Name="check" Margin="3" VerticalAlignment="Center" Click="CheckBox_Click" IsChecked="{Binding IsChecked, Mode=TwoWay}" />
<TextBox x:Name="tb" BorderThickness="0" Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="btn" Content="Save" Click="btn_Click"/>
</StackPanel>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
namespace ListBoxWithCheckBoxTemplateDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
var cb = sender as CheckBox;
var item = cb.DataContext;
list.SelectedItem = item;
}
private void btn_Click(object sender, RoutedEventArgs e)
{
string constr = @"constr ";
SqlConnection con = new SqlConnection(constr);
con.Open();
foreach (Data item in list.Items)
{
if (item.IsChecked == true)
{
string sqlinsert = "INSERT INTO [dbo].[Table]([Id],[Name])VALUES(@Id,@Name)";
SqlCommand cmd = new SqlCommand(sqlinsert, con);
cmd.Parameters.AddWithValue("@Name", item.Name);
cmd.Parameters.AddWithValue("@Id", item.Id);
SqlCommand cmdCount = new SqlCommand("SELECT count(*) from [dbo].[Table] WHERE Id = @Id", con);
cmdCount.Parameters.AddWithValue("@Id", item.Id);
int count = (int)cmdCount.ExecuteScalar();
if (count == 0)
{
cmd.ExecuteNonQuery();
MessageBox.Show("item saved");
}
else
{
MessageBox.Show("item exists");
}
}
}
con.Close();
}
}
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
Items = new ObservableCollection<Data>();
Items.Add(new Data() { Id= 1, Name = "Beijing", IsChecked = false });
Items.Add(new Data() { Id = 2, Name = "Shanghai", IsChecked = false });
Items.Add(new Data() { Id = 3, Name = "Guangdong", IsChecked = true });
Items.Add(new Data() { Id = 4, Name = "Beijing", IsChecked = true });
}
private ObservableCollection<Data> _items;
public ObservableCollection<Data> Items
{
get => _items;
set
{
_items = value;
OnPropertyChanged(nameof(Items));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Data
{
public int Id { get;set;}
public string Name { get; set; }
public bool IsChecked { get; set; }
public override string ToString() => Name;
}
}
The result:

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html