在我的Maui XAML中,我有一个最初启用的Entry控件,一旦用户在该控件中输入了正确的值,我希望以编程方式禁用它.

但事实并非如此

我的xaml代码看起来像这样

 <Entry
     x:Name="InputAnswer"
     Margin="0,0,20,0"
     FontSize="24"
     HorizontalOptions="Start"
     IsEnabled="{Binding IsWrong}"
     Keyboard="Numeric"
     MaxLength="5"
     Placeholder=""
     Text="{Binding InputAnswer}" />

我的模特班

  public class EquationWithInput: Equation, INotifyPropertyChanged
  {
      public int? InputAnswer {  get; set; }
      public int CorrectAnswer{  get; set; }
      public bool IsWrong { get => InputAnswer == null ? true : InputAnswer != CorrectAnswer; }
      ...
  }

但当我判断结果时,该控件从未被禁用.绑定似乎不起作用,但对于所有其他属性,它在此页面中的其他所有位置都有效.例如,InputAnswer人.

那么我需要做些什么才能让这件事起作用呢?

推荐答案

您需要确保EquationWithInput类正确实现INotifyPropertyChanged接口,并在InputAnswer更改时引发PropertyChanged事件,因为这会间接影响IsWrong属性.

以下是如何修改EquationWithInput类,以确保对InputAnswer的更改也正确地通知UI有关IsWrong属性的更新:

public class EquationWithInput : Equation, INotifyPropertyChanged
{
    private int? inputAnswer;
    public int? InputAnswer
    {
        get { return inputAnswer; }
        set
        {
            if (inputAnswer != value)
            {
                inputAnswer = value;
                OnPropertyChanged(nameof(InputAnswer));
                // Since IsWrong depends on InputAnswer, it needs to notify the change as well.
                OnPropertyChanged(nameof(IsWrong));
            }
        }
    }

    public int CorrectAnswer { get; set; }

    public bool IsWrong => InputAnswer == null || InputAnswer != CorrectAnswer;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

Csharp相关问答推荐

System.Data.SQLite:判断SQLite数据库是否为空(任何表中至少有一行)

如何使嵌套for-loop更高效?

为什么Blazor值在更改后没有立即呈现?

为什么在ANTLR4中会出现不匹配的输入错误?""

. net依赖注入如何避免服务类中的新

如何注册接口类型,类型<>

(乌龙)1&#比c#中的UL&#慢吗?

SortedSet.Remove()不会删除SortedSet.Min返回的元素

如何使用C#获取FireStore中的列表输出文档

C#普罗米修斯指标

从VS调试器而不是测试资源管理器运行的调试NUnitDotNet测试

如何在C#中转换泛型包装类内部的派生类

记录类型';==运算符是否与实现IEquatable<;T&>;的类中的';equals&>方法执行等价比较?

在C#ASP.NET内核中使用INT AS-1进行控制器场景的单元测试

如何在.NET Core 8中自定义标识用户模型

将两个for循环更改为一条LINQ语句

如何在特定时间间隔运行多个后台任务?

NETSDK1201:对于面向.NET 8.0和更高版本的项目,默认情况下,指定RUNTIME标识符将不再生成自包含的应用程序

无效的Zip文件-Zip存档

读取测试项目中的应用程序设置