hi! i have a listbox binded to a list. the list contains checkboxes binded to a field/member of the list. what i want to achieve is that i want to delete the data from list when it's corresponding checkbox is checked...

here's my xaml code:

<ListBox Name="ListBox1" ItemsSource="{Binding histList, Mode=OneWay}" Margin="10,10,10,10" Height="197" VerticalAlignment="Center" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding FilterName, Mode=TwoWay}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

here's the code in the .cs file

namespace ReportsUIScreens
{
    public partial class EditStyle : ChildWindow
    {
        private List<filterHistory> histList;
        public EditStyle(List<filterHistory> histListLink)
        {
            InitializeComponent();
            histList = histListLink;
            this.ListBox1.ItemsSource=histList;
        }
 
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //HERE'S WHERE I WANT TO DELETE
            this.DialogResult = true;
        }
        .........
        .........
        .........
    }
}

please help me...

Dani AI

Generated

— the core issues are (1) your CheckBox must bind its IsChecked to a boolean on the item (not bind Content and expect checked state), and (2) the list type must support change notifications so the UI updates when you remove items. is right that you should loop and remove checked items, but don’t remove while iterating the collection directly.

Make the item model raise PropertyChanged and expose a bool (example name IsChecked). Use an ObservableCollection for the ItemsSource so removes show up in the ListBox automatically.

Example model (implements INotifyPropertyChanged):

public class FilterHistory : INotifyPropertyChanged
{
    public string FilterName { get; set; }

    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set { if (_isChecked != value) { _isChecked = value; OnPropertyChanged("IsChecked"); } }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

Bind the CheckBox IsChecked to that property (Content can still show the name):

<CheckBox Content="{Binding FilterName}" IsChecked="{Binding IsChecked, Mode=TwoWay}" />

Safe removal on OK click (avoid modifying while enumerating):

var toRemove = HistList.Where(h => h.IsChecked).ToList();
foreach (var item in toRemove) HistList.Remove(item);

Notes and cautions: if you stick with List<T> you must either reassign ItemsSource after removing or use RemoveAll and then refresh the binding; ObservableCollection<T> is the cleaner option. Also TwoWay on Content does not propagate checkbox state — IsChecked is the property that must be two-way bound.

Hi,
one thing: a "list" as you have stated - it canNOT have checkBoxes at all. It can only contain some boolean values.

So, in your code you have a generic list<T> with a custom class "filterHistory". This is a class which has a boolan property - for the checkBox checked/uncheced if I`m not wrong.

I hope I`m not wrong - but this is only my presuming, regarding your code, so we can do the following: lets loop through the list, and check for that boolean property - if value is true - delete the data (if false, do notihing; dont delete the data):

for(filterHistory item in histList)
{
     if(item.checkUncheck)
     {
         //here item is true! so you can delete the data!!
     }
}

//your filterHistory class //this is only my predistion how it looks:
class filterHistory
{
     public bool checkUncheck { get; set; }
     //and the rest
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.