开发者

How to accomplish property and control update in wpf?

I have wpf application with 3 textboxes.

txb1 is for inserting text

txb2 displays certain value from inserted parsed text, it is i开发者_StackOverflow中文版nt number

txb3 should display value from txb2 multiplyed by constant value

There is a class storing data.

class Data
{
    private conts int Mul = 10;
    public string Text {get; set;}
    public int Number {get; set;}
    private int MultiNumber
    {
        get
        {
            Number * Mul;
        }
    }

    public string MultiNumberFormated
    {
        get
        {
            string.Format("{some format}", MultiNumber);
        }
    }
}

By clicking button in form I handle event, create new instance of Data, pass txb1.Text through constructor and call parsing function inside Data class, which set value to Number. Textboxes are defined with Text="{Binding SomeProperty}" and DataContext of container with textboxes is set on Data instance, so after button click values appers in corresponding boxes.

Now, I need a solution to make txb3 update when value in txb2 is changed.

This is a small example of complex application, with more chained textboxes. For example txb4 displays multiplyed MultiNumber, so I don't want winforms solution using button event to update. Is there such a way? I'd also appreciate code sample sitting for this specific application.


As said by Slaks, you will have to implement INotifyPropertyChanged in your ViewModel.
We are referring to the MVVM pattern, with a particular focus on the V (View) and VM (ViewModel).

You can find an example here

Then once you documented yourself on that, you actually want to use Mode=TwoWay on the binding of the TextBoxes that need to take input from your users.

Then in the getters of your string properties that populate the other textboxes (those who depend upon each parameters), you implement the logic that does the calculation.
In their setters, you will need to raise the PropertyChanged event.

You will quickly realize that WPF is tighly coupled with this interface, and the MVVM pattern.

Now once you did all that, I'll still be there to answer more specific questions ;-)


You have a couple of options here. You will need to implement INotifyPropertyChanged on your Data object as SLaks said - there are plenty of tutorials on that - then you can either

  • Make a new property on your Data class for the multiplied number and bind txb3 to that
  • Implement a value converter to handle the multiplication for you, if it's going to be display only

I would also get away from your current approach of passing in values from a text box in a button handler.

I'd probably look at moving your MultiNumberFormatted function into a value converter for txb2 and databinding everything (including txb1 which is currently your entry point to the calculation and is not bound to anything?) to properties on an instance of Data created much sooner in the lifecycle - either constructed with the page or passed in.

Or, you could make MultiNumberFormatted a property on Data and set it explicitly from the setter of your initial number:

int InputNumber
{
    get
    {
        _return _inputNumber;
    }
    set
    {
        _inputNumber = value;
        MultiNumberFormatted=String.Format("whatever: {0}", InputNumber);   
        NotifyPropertyChanged("InputNumber");
    }
}

string MultiNumberFormatted
{
    get
    {
        return _multiNumber;
    }
    set
    {
        _multiNumber=value;
        NotifyPropertyChanged("MultiNumberFormatted");
    }
}

That's one way of doing it, there are others. The point is that if you bind everything to appropriate properties and let the WPF binding infrastructure do its thing, it's all 'live' and everything "just works".


You need to implement INotifyPropertyChanged in your model and raise the PropertyChanged event in all dependent property setters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜