简体   繁体   English

WPF绑定问题与更新值

[英]WPF Binding issue with updating values

I have a .xaml file and a .cs file that share value with Binding. 我有一个.xaml文件和一个.cs文件,它们与Binding共享价值。

To make it simples, I have 1 button and 1 textbox. 为了简单起见,我有1个按钮和1个文本框。 I want to button to be disabled when the textbox's text has no character. 当文本框的文本没有字符时,我想禁用按钮。

Here is the two codes of the xaml for binding: 这是xaml用于绑定的两个代码:

  <TextBox Name="txtSend" Text="{Binding Path=CurrentText,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    <Button IsEnabled="{Binding Path=IsTextValid}"  Name="btnSend">Send</Button>

The two properties in the .cs file look like that: .cs文件中的两个属性如下所示:

    public string CurrentText
    {
        get
        {
            return this.currentText;
        }
        set
        {
            this.currentText = value;
            this.PropertyChange("CurrentText");
            this.PropertyChange("IsTextValid");
        }
    }

    public bool IsTextValid
    {
        get
        {
            return this.CurrentText.Length > 0;
        }
    }

The this.PropertyChanged is simply a method that call PropertyChanged from INotifyPropertyChanged. this.PropertyChanged只是从INotifyPropertyChanged调用PropertyChanged的方法。

The problem is that I have to call the this.PropertyChange("IsTextValid"); 问题是我必须调用this.PropertyChange("IsTextValid"); in the Setter of the CurrentText to be able to have the button state change. 在CurrentText的Setter中可以更改按钮状态。

Question 1) Is it the good way to do it... if the rules become more complexe I might require to call a lot of PropertyChanged...? 问题1)这是个好方法吗...如果规则变得更加复杂,我可能需要调用很多PropertyChanged ...?

Question 2) My button is enable when the form load. 问题2)我的按钮在表单加载时启用。 How can I make it check the method from the start? 如何从一开始就检查方法?

Question 1: This is correct. 问题1:这是正确的。 There is no problem doing that. 这样做没有问题。 However you could take a look at the validation using the IDataErrorInfo. 但是,您可以看看使用IDataErrorInfo进行的验证。 (Google search for it, and you will find a lot of good examples) (Google搜索它,您会发现很多很好的例子)

Question 2: make sure your "currentText" string is initialized with string.empty. 问题2:确保使用string.empty初始化您的“ currentText”字符串。 Because if you did not initialized it, it will be null, and the getter for IsTextValid will throw an exception, and WPF will fail to retrieve the value. 因为如果未初始化它,它将为null,并且IsTextValid的getter将引发异常,并且WPF将无法检索该值。

Or do it like that: 或那样做:

public bool IsTextValid
{
    get
    {
        return ! string.IsNullOrEmpty( this.CurrentText );
    }
}

Your way of doing it is correct. 您的操作方式是正确的。 If you are a bit lazy (as I am), you should have a look on the NuGet package Fody.PropertyChanged. 如果您有点懒惰(像我一样),则应该看看NuGet包Fody.PropertyChanged。

Your code would simplify to 您的代码将简化为

public string CurrentText { get; set; }

public bool IsTextValid { get { return this.CurrentText.Length > 0; } }

Fody.PropertyChanged does the rest for you. Fody.PropertyChanged为您完成其余工作。 It automatically adds the needed instructions to notify that CurrentText changed, and it even detects that IsTextValid depends on CurrentText and notifies for it, too. 它会自动添加所需的指令,以通知CurrentText发生了更改,甚至检测IsTextValid依赖于CurrentText并为此进行通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM