1

I have a problem with a method. I have checkboxes to change the value of variables to true / false.

My variables Prise1, Prise2, Prise3 and Prise4 on one side, and on the other side on the 7 days of the week.

I have to complete a request that is made in relation to the selected days. For example:

example

On the screenshot, I would like to modify the information about:

  • Tuesday, Wednesday of "prise1"
  • nothin on "prise2"
  • on Friday and Sunday of "prise3"
  • Thursday of "duration"

I thought of a 2D table that I could go through to check if the values are true / false, but I don't really know how to do it.

Edit : Did i miss something ? Binding doesn't work, when i check i'm not going into the "set".

    public class WeekValues : ObservableObject
{
    public bool Monday { get; set; }
    public bool Tuesday { get; set; }
    public bool Wednesday { get; set; }
    public bool Thursday { get; set; }
    public bool Friday { get; set; }
    public bool Saturday { get; set; }
    public bool Sunday { get; set; }
}

For the viewModel :

        private WeekValues _Prise1;
    public WeekValues Prise1
    {
        get
        {
            return _Prise1;
        }
        set
        {
            if (value != _Prise1)
            {
                _Prise1 = value;
                RaisePropertyChanged(nameof(Prise1));
            }
        }
    }

    private WeekValues _Prise2;
    public WeekValues Prise2
    {
        get
        {
            return _Prise2;
        }
        set
        {
            if (value != _Prise2)
            {
                _Prise2 = value;
                RaisePropertyChanged(nameof(Prise2));
            }
        }
    }

and WPF :

                    <StackPanel HorizontalAlignment="Center" Grid.Row="1" Grid.Column="2">
                    <CheckBox Margin="0,9,0,5" IsChecked="{Binding Prise1}"></CheckBox>
                    <CheckBox Margin="0,10,0,5" IsChecked="{Binding Prise2.Monday}"></CheckBox>
                    <CheckBox Margin="0,10,0,5"></CheckBox>
                    <CheckBox Margin="0,9,0,0"></CheckBox>
                </StackPanel>
                <Label Grid.Row="0" Grid.Column="3" HorizontalAlignment="Center" FontWeight="Bold">M</Label>
                <StackPanel HorizontalAlignment="Center" Grid.Row="1" Grid.Column="3">
                    <CheckBox Margin="0,9,0,5" IsChecked="{Binding Prise1.Tuesday}"></CheckBox>
                    <CheckBox Margin="0,10,0,5" IsChecked="{Binding Prise2.Tuesday}"></CheckBox>
                    <CheckBox Margin="0,10,0,5"></CheckBox>
                    <CheckBox Margin="0,9,0,0"></CheckBox>
                </StackPanel>

Thank you in advance

Sephystos
  • 175
  • 1
  • 9

1 Answers1

2

How about creating a custom type instead of an array in your ViewModel?

internal class WeekValues : INotifyPropertyChanged
{
    public bool Monday { get {... } set { ...} }
    public bool Tuesday { get {... } set { ...} }
    public bool Wednesday { get {... } set { ...} }
    public bool Thursday { get {... } set { ...} }
    public bool Friday { get {... } set { ...} }
    public bool Saturday { get {... } set { ...} }
    public bool Sunday{ get {... } set { ...} }

    ...
}

internal class MyViewModel : INotifyPropertyChanged
{
    public WeekValues Prise1 { get {... } set { ...} }
    public WeekValues Prise2 { get {... } set { ...} }
    public WeekValues Prise3 { get {... } set { ...} }
    public WeekValues Duree { get {... } set { ...} }

    ...
}

Than for each CheckBox you could write:

<CheckBox IsChecked="{Binding Prise1.Monday}" />

But a better solution would be to create a UserControl for all week values with 7 CheckBoxes in it. The UserControl could have a DependencyProperty of type WeekValues. This way your main xaml could look like this:

<MyWeekValuesUserControl WeekValues="{Binding Prise1}" />
<MyWeekValuesUserControl WeekValues="{Binding Prise2}" />
<MyWeekValuesUserControl WeekValues="{Binding Prise3}" />
<MyWeekValuesUserControl WeekValues="{Binding Duree}" />

What is more, if Prise1, Prise2, Prise3, Duree list is dynamic, you could use an ItemsControl.

How to create an UserControl you can find in this blog post

Jonatan Dragon
  • 4,675
  • 3
  • 30
  • 38
  • The `WeekValues` class is definitely the way to go. UI-wise, I'd probably use an `ItemsControl` with `SharedSizeGroup` grids instead of a user control. – Heinzi May 20 '19 at 13:20
  • Your solution seems to be the right one even if it makes me add a class to the project I'm on, it must be possible. but if you tell me that the best thing would be to use a "UserControl", I'll see if I can understand how it works. Thank you for the link! – Sephystos May 20 '19 at 13:21
  • Doesnt work for me right now, maybe i miss something ? the biding doesn't seem to work, nothing is happening. I may have made a mistake, I'm editing my code, could you please look at it? – Sephystos May 20 '19 at 14:14
  • Sure. In WeekValues you should also call RaisePropertyChanged(). – Jonatan Dragon May 20 '19 at 14:22
  • Another hint: As I can see, you have ObservableObject. If it comes from one of known frameworks, you could probably use Set method instead of calling RaisePropertyChanged. Take a look here when you figure it all out :) http://dotnetpattern.com/mvvm-light-toolkit-example – Jonatan Dragon May 20 '19 at 14:22
  • Also, some more examples of CheckBox binding: https://stackoverflow.com/a/31029460/1319086 . If this is your first time with Binding, maybe try with a simpler example. Something like a single window with a single view model and a single checkbox :) – Jonatan Dragon May 20 '19 at 14:26
  • I think this question is answered completly. If possible, try to ask a new question for your next trouble, to avoid editing this one. – Jonatan Dragon May 20 '19 at 14:29