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

2
  • Is it windows forms or wpf? Commented May 20, 2019 at 12:15
  • it's wpf, with MVVM Commented May 20, 2019 at 12:25

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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.
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!
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?
Sure. In WeekValues you should also call RaisePropertyChanged().
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 :) dotnetpattern.com/mvvm-light-toolkit-example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.