【问题标题】:How to clear the numeric type textboxes on Button click in View model using Icommand如何使用 Icommand 在视图模型中清除按钮单击上的数字类型文本框
【发布时间】:2020-09-25 06:45:57
【问题描述】:

整数类型的Id和Age应该写什么?我应该如何清除表格中的字段? Xaml 代码

    <Button Command="{Binding CmdClear}" Content="Clear"></Button>

视图模型

public ICommand CmdClear
        {
            get
            {
                return new DelegateCommand(ClearFields);
            }
        }

        private void ClearFields()
        {
            Id =;
          Name = string.Empty;
            Age = ;
            Country = string.Empty;
            Active = string.Empty;

        } 

【问题讨论】:

  • 如果“0”是有效值,则将源属性值设置为“0”或“-1”。然后使用 IValueConverter 将此值转换为空字符串或一些占位符文本。

标签: c# .net wpf textbox wpf-controls


【解决方案1】:

将其设为可空字段,例如 Id? 和 Age?。
或者
您可以在 XAML 中设置该字段属性 TargetNullValue=''

【讨论】:

  • 我无法将任何 TargetNullProperty 添加到 Xaml 代码。该字段实际上是数字文本框。您能否详细说明如何使变量为 Nullable?
  • 我得到了答案。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-06-13
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多