Sephystos
Sephystos

Reputation: 69

how to get the objects from an observable collection?

I don't know if it's possible but I would like to add objects to an observable collection and reuse them later.

I have this in wpf:

 <CheckBox Margin="0,9,0,5" IsChecked="{Binding Prise1.Monday, Mode=TwoWay}"/>
 <CheckBox Margin="0,10,0,5" IsChecked="{Binding Prise2.Monday, Mode=TwoWay}"/>
 <CheckBox Margin="0,10,0,5" IsChecked="{Binding Prise3.Monday, Mode=TwoWay}"/>
 <CheckBox Margin="0,9,0,0" IsChecked="{Binding Durée.Monday, Mode=TwoWay}"/>

For all days of the week. Class WeekValues :

    private bool _Monday;
    public bool Monday
    {
        get
        {
            return _Monday;
        }
        set
        {
            _Monday = value;
            RaisePropertyChanged(nameof(Monday));
        }
    }
    private bool _Tuesday;
    public bool Tuesday
    {
        get
        {
            return _Tuesday;
        }
        set
        {
            _Tuesday = value;
            RaisePropertyChanged(nameof(Tuesday));
        }
    }
    private bool _Wednesday;
    public bool Wednesday
    {
        get
        {
            return _Wednesday;
        }
        set
        {
            _Wednesday = value;
            RaisePropertyChanged(nameof(Wednesday));
        }
    }
    private bool _Thursday;
    public bool Thursday
    {
        get
        {
            return _Thursday;
        }
        set
        {
            _Thursday = value;
            RaisePropertyChanged(nameof(Thursday));
        }
    }
    private bool _Friday;
    public bool Friday
    {
        get
        {
            return _Friday;
        }
        set
        {
            _Friday = value;
            RaisePropertyChanged(nameof(Friday));
        }
    }
    private bool _Saturday;
    public bool Saturday
    {
        get
        {
            return _Saturday;
        }
        set
        {
            _Saturday = value;
            RaisePropertyChanged(nameof(Saturday));
        }
    }
    private bool _Sunday;
    public bool Sunday
    {
        get
        {
            return _Sunday;
        }
        set
        {
            _Sunday = value;
            RaisePropertyChanged(nameof(Sunday));
        }
    }
}

And i want to do something like this:

   public ObservableCollection<WeekValues> allPrises = new ObservableCollection<WeekValues>();

   Prise1 = new WeekValues();
   Prise2 = new WeekValues();
   Prise3 = new WeekValues();
   Durée = new WeekValues();

   allPrises.Add(Prise1);
   allPrises.Add(Prise2);
   allPrises.Add(Prise3);
   allPrises.Add(Durée);

And use a method like :

MyMethod(allPrises)
{
    foreach(var X in allPrises)
       {
           if(Prise1.Monday) ...do something
           if(Prise2.Monday) ... do something else
       }

Is this clear? So that's the idea, I'd like to know how to proceed, because when I make a foreach I only get trues / falses without "Prise1" or "Prise2". Would you know how to solve this? Thank you in advance

Upvotes: 0

Views: 79

Answers (1)

mm8
mm8

Reputation: 169270

You are iterating over all 4 WeekValues. So X is Prise1 in the first iteration and so on.

if (Prise2.Monday) could be written as if (allPrises[1].Monday) if you never change the order of the elements.

Upvotes: 1

Related Questions