【问题标题】:Boolean and Array trouble布尔值和数组问题
【发布时间】:2019-10-06 19:34:38
【问题描述】:

我的方法有问题。 我有复选框可以将变量的值更改为真/假。

我的变量 Prise1、Prise2、Prise3 和 Prise4 位于一侧,另一侧位于一周的 7 天。

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

在截图上,我想修改以下信息:

  • “prise1”的星期二、星期三
  • “prise2”没有任何内容
  • “prise3”的周五和周日
  • “持续时间”的星期四

我想到了一个二维表,我可以通过它来检查值是真还是假,但我真的不知道该怎么做。

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

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

对于视图模型:

        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>

提前谢谢你

【问题讨论】:

  • 是 windows 窗体还是 wpf?
  • 它是 wpf,带有 MVVM

标签: c# arrays wpf boolean


【解决方案1】:

如何在 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}" />

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

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

此外,如果 Prise1、Prise2、Prise3、Duree 列表是动态的,您可以使用 ItemsControl

如何创建UserControl,您可以在this blog post 中找到

【讨论】:

  • WeekValues 类绝对是要走的路。在 UI 方面,我可能会使用 ItemsControlSharedSizeGroup 网格而不是用户控件。
  • 您的解决方案似乎是正确的,即使它让我在我正在进行的项目中添加一个类,它一定是可能的。但是如果你告诉我最好的办法是使用“UserControl”,我会看看我是否能理解它是如何工作的。谢谢你的链接!
  • 现在不适合我,也许我错过了什么?投标似乎不起作用,什么也没有发生。我可能犯了一个错误,我正在编辑我的代码,你能看一下吗?
  • 当然。在 WeekValues 中,您还应该调用 RaisePropertyChanged()。
  • 另一个提示:如我所见,你有 ObservableObject。如果它来自已知框架之一,您可能会使用 Set 方法而不是调用 RaisePropertyChanged。当你想清楚的时候看看这里:) dotnetpattern.com/mvvm-light-toolkit-example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 2011-04-18
  • 2019-09-14
  • 2015-10-07
  • 2015-09-17
  • 2013-11-28
相关资源
最近更新 更多