简体   繁体   中英

Get the IsChecked property value from a list of checkbox in a combobox

I am writing a program in which I have a combobox made of a series of checkbox. The user can select as many checkboxes as he want. After taht, when he click on a button, I want to check al the checkboxes to see which are selcted and which are not.

But I don't have any idea of how to do that. I tried many ways, but nothing of them returns me the value of the property. In fact, I'm not able to reach the binding property from the code. Here is the code:

In WPF:

<ComboBox x:Name="month_comboBox" Margin="349,107,0,0" Height="22" VerticalAlignment="Top" Visibility="Hidden">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Width="20" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"/>
                <TextBlock Text="{Binding Path=month_Name}" Width="100" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

In the .cs file, this is the code to load the combobx:

In the main class there is this method:

private void LoadMonths()
    {
        MonthList ML = new MonthList();
        month_comboBox.ItemsSource = ML.Months;
        month_comboBox.SelectedIndex = 0;
    }

and there are also this two classes for the elements:

public class MonthList
    {
        ObservableCollection<Month> _Months = new ObservableCollection<Month>();

        public MonthList()
        {
            _Months.Add(new Month { month_Name = "every month" });
            _Months.Add(new Month { month_Name = "january" });
            _Months.Add(new Month { month_Name = "february" });
            _Months.Add(new Month { month_Name = "march" });
            _Months.Add(new Month { month_Name = "april" });
            _Months.Add(new Month { month_Name = "may" });
            _Months.Add(new Month { month_Name = "june" });
            _Months.Add(new Month { month_Name = "july" });
            _Months.Add(new Month { month_Name = "august" });
            _Months.Add(new Month { month_Name = "september" });
            _Months.Add(new Month { month_Name = "october" });
            _Months.Add(new Month { month_Name = "november" });
            _Months.Add(new Month { month_Name = "december" }); 
        }

        public ObservableCollection<Month> Months { get { return _Months; } }

    }

    public class Month
    {
        private string monthName;

        public bool IsSelected {
            get { return IsSelected; }
            set; 
        }

        public string month_Name
        {
            get { return monthName; }
            set { monthName = value; }
        }

    }

Now, what I want is a way to know, for every checkbox in the combobox, if the checkbox is selected or not.

I have this for, but i don't know what instruction put insisde it to obtain the property value:

for (int i = 0; i < month_comboBox.Items.Count; i++) { 
    //Instruction missing here
}

I know that I have to use something similat to this

month_comboBox.Items[i]

but i don't know what I have to hook to it to obtain the property value.

Thank you all

This is pretty trivial - cast it like this:

public void GetValues()
{
    foreach(var item in month_comboBox.Items)
    {
        Month temp = item as Month;
        bool isSelected = temp.IsSelected;
        //continue here :)
    }
}

You can also use pattern matching.

        if (item is Month month)
        {
            var isSelected = month.IsSelected;
            //continue here :)
        }

Or Maybe like this:

    public IEnumerable<Month> GetValues()
    {
        foreach(var item in month_comboBox.Items)
        {
            if(item is Month month) return month;
        }
    }

Also please check out MVVM here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM