布尔和数组问题

时间:2019-05-20 12:12:28

标签: c# arrays wpf boolean

我对方法有疑问。 我有复选框将变量的值更改为true / false。

我的变量Prise1,Prise2,Prise3和Prise4在一周的7天的一侧,而在另一侧。

我必须完成与所选日期有关的请求。例如:

example

在屏幕截图上,我想修改有关以下信息:

  • “ prise1”的星期二,星期三
  • nothin在“ prise2”上
  • “ prise3”的星期五和星期日
  • “持续时间”星期四

我想到了一个2D表,可以检查该表的值是否为true / false,但我真的不知道该怎么做。

编辑: 我错过了什么 ?绑定不起作用,当我检查我未进入“设置”时。

    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; }
}

对于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));
            }
        }
    }

和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>

提前谢谢

1 个答案:

答案 0 :(得分:2)

如何在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 { ...} }

    ...
}

比起每个CheckBox,您可以写:

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

但是更好的解决方案是为其中包含7个UserControl的所有星期值创建一个CheckBoxesUserControl可以具有类型DependencyProperty的{​​{1}}。这样,您的主要xaml可能如下所示:

WeekValues

此外,如果Prise1,Prise2,Prise3,Duree列表是动态的,则可以使用<MyWeekValuesUserControl WeekValues="{Binding Prise1}" /> <MyWeekValuesUserControl WeekValues="{Binding Prise2}" /> <MyWeekValuesUserControl WeekValues="{Binding Prise3}" /> <MyWeekValuesUserControl WeekValues="{Binding Duree}" />

如何创建this blog post中的ItemsControl