2

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

Massimo P.
  • 55
  • 1
  • 10
  • Have you tried casting `Items[i]` to a `CheckBox` and hooking to the `Checked property`? – CDove Jun 28 '17 at 15:08
  • Yes, but my problem is that i can't obtain a reference from the month_comboBox.Items[i] to the checkBox inside it. When I use month_comboBox.Items[i], I can't find anything that, from here, conduct me to the IsChecked property of the checkBox. – Massimo P. Jun 28 '17 at 15:15
  • Looking at `MonthList` you are half-way into MVVM. In MVVM wonderful world you don't deal with `CheckBox`es or `ComboBox`es, you deal with right things, e.g. months. When user clicks the button and the `ICommand` will execute, there you simply check `Month` collection to find which months are `IsSelected`. – Sinatr Jun 28 '17 at 15:22
  • Then feel free to mark my answer :) – Peter Jun 29 '17 at 10:02

1 Answers1

3

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

Peter
  • 1,655
  • 22
  • 44