1

I have a property in my view model which returns a constant under some conditions.

which is implemented similiar to this:

    class Tmp : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public String Text
        {
            get { return "testing"; }
            set
            {
                PropertyChanged(this,new PropertyChangedEventArgs("Text"));                }
        }
    }

so the property Text alwasys returns "testing".

I bound this to a text box like :

  <TextBox Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

When the application starts, the textbox correclty says testing.

now when I type something in the text box the setter gets called, which calls PropertyChanged.

After this something ( probably the GUI ) calls the getter and gets the value "testing".

However the text in the textbox is not changed back to testing.

So I can type "abc" into the text box, and text box displays "abc" even though the model is just storing "testing".

why isnt the text in the text box not reset to "testing" at each keystroke?

6
  • 1
    IIRC don't you have to pass the property name through the propertychanged event rather than an empty string? Commented Jun 15, 2012 at 16:16
  • Not really String.Empty or "" means "Hey, all my properties are invalid, please get them all" in this case it means "Get text aswell" ... its to much, but working. Commented Jun 15, 2012 at 16:18
  • Empty sting should force everything to get updated.. I originally had the property name.. changed to "" just in case Commented Jun 15, 2012 at 16:19
  • changed the code but still getting the same behaviour Commented Jun 15, 2012 at 16:21
  • I just tried this (.NET 4.5) and it worked as you expect Commented Jun 15, 2012 at 16:24

1 Answer 1

6

Why should the textbox get the text again? It just wrote it into your source property it "knows" that it must be the same, because he is in the middle of telling the property the new value. Otherwise you would create circular references. What you are doing is going completely against guidelines and behavior expected from a property.

Remove the setter, make the binding one way, raise the text property when your constant is changed, and make the textbox readonly. Remember, its not necessary to call Propertychanged in the setter.

To make your initial approach work, you need to break out of the "Textbox changes property but won't listen for incoming raises" state

set
{
   sUIDispatcher.BeginInvoke((Action)(() => Raise("Name")));
}

i would like to add, that this is a REALLY bad idea and strongly discourage you to do it like that.

Sign up to request clarification or add additional context in comments.

4 Comments

this is a simplified version of the original problem so i cant do one way binding.. I could potentially disable the text box but that will involve either significant refactoring or multi converter.
the reason I thought it would work was because something is calling my getter. Since there is noting besides the textbox in this example I thought text box was reading the "Text" property
Like i said it does reading the text property. but when it updates because of text entered into the textbox, it "disables" listening for raises because he expects that the next incoming raise is going to be exactly what the textbox told the property. See my answer i added a code snippet, which works like you might want.
I was already doing an alternate implementation to get rid of this issue. I just asked this question out of curiosity. Thanks for explaining this issue. I would never have been able to figure it out myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.