views:

448

answers:

3

I have a an object which is set to the DataContext in a Window. I have textboxes in the window which are bound to the properties on the object. There seems to be a delay however before the properties on the object are updated.

<TextBox x:Name="txtPropertyOne" Text="{Binding Path=PropertyOne,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  />

If I change the values in a few textboxes then quickly try to access the properties to which they map, sometimes there are changes which aren't reflected in the properties of the object. I thought that was what the PropertyChanged UpdateSourceTrigger was supposed to take care of.

A: 

Is your object a DependencyObject? Is the property you're binding to a DependencyProperty? If they are, you don't need to specify the trigger. If they aren't, I'd suggest making them so. Its the easiest way to create a reliably bindable object.

I think the delays are related to your use of the UpdateSourceTrigger. I don't think it does what you think it does, which is why its not behaving as you expect.

Drop the UST, and if that doesn't work refactor your binding object to be a DO and PropertyOne to be a DP. Trust me, once you get through the pains of learning DependencyObjects, its well worth it.

Will
Care to explain why?
Will
Why would the delays be related to the UpdateSourceTrigger. What does he think it does? What do you think it does? You can easily bind a textbox to a non dependency property and the XAML above should work. Also, what if his class lives in a separate library that he can't modify?
siz
I can't answer what he thinks or what he's doing. I'm trying to go on what information he gave. Apparently he's experiencing multithreading issues. He didn't mention he was multithreading. Also, what if every what if ever?
Will
My problems with this:1. You don't explain what UST does, how he might be using it incorrectly. 2. Doesn't matter whether it is a DO or not. The XAML above should work, it doesn't.3. If he drops the UST, it definitely won't work.You're right, my what if was contrived.
siz
1. I'm going on a single line of xaml, trying not to make too many assumptions about his code 2. OMG his UST only works if he's implementing INotifyPropertyChanged; we don't KNOW he is 3. Bullcrap. It only works with INPC; doesnt work with pocos and isn't needed with DPs
Will
You are wrong Will. WPF bindings will bind to any CLR property. Depending on the BindingMode different things are required of that property. (ie, TwoWay requires a getter/setter on the property). TextBox binding to a property works just fine as long as there is a setter defined. Try it out in VS.
siz
Take a look at this: http://rafb.net/p/eumGr283.html. The value gets propagated to the Data object just fine without INPC.
siz
You misread me. I said that the UST only works with the INPC; the PropertyChanged update source trigger eminates from the INPC. Bindings ignore the UST if your objects don't implement the interface.
Will
What you are saying is true for the Destination property/object. UST determines what action causes an update on the Source property from the Destination object. If it is PropertyChanged, the destination has to be DO or INPC, you are correct. But this isn't the case in the original question.
siz
True. I thought it was useless, and that maybe a side effect was causing his odd behavior. Remote chance, yeah, but I figured it wouldn't do any harm. DO's are pretty much bulletproof when it comes to binding, so that was my second recom'd. Not the solution, but not worthy of -2 imho.
Will
+1  A: 

If I change the values in a few textboxes then quickly try to access the properties to which they map

I can interpret this statement in two ways:

  1. You're trying to access the values on a background thread. In that case, you may be accessing the properties before the UI thread has had a chance to do its thing.
  2. You're using a separate message on the UI thread to check the values. Bindings are updated at a priority lower than Send and Normal. So if your message is priority Send or Normal it will be processed before any pending binding updates.

If this doesn't answer your question, please clarify what you mean by "quickly trying to access the properties".

HTH, Kent

Kent Boogaart
That's what I was thinking. With the object being the DataContext, I'm accessing it from a BackgroundWorker, which may be why it doesn't get chance to update. I'll try calling UpdateSource on the bindings before starting the BackgroundWorker.
Echilon
A: 

The basic rule of WPF Databinding is simple:

  • The target property must be a dependency property, and you're already correct, it's bound to Text property of TextBox.
  • The source property can be a CLR object (other than any derived WPF's DependencyObject), but the object must employ or implement its own INotifyPropertyChanged.

Have you you already implemented INotifyPropertyChanged on your object?

eriawan
All of the object's properties fire NotifyPropertyChanged events. I think Kent Boogart might have nailed it with accessing from another thread before the UI has updated the object's properties.
Echilon
Yes, but are sure your objects have implemented the INotifyPropertyChanged interface? Kent Boogart answer touches different view and his answer is different than mine.
eriawan
Just a comment on point two: the INotifyPropertyChanged implementation matters _only_ if Echilon is doing clrObject.PropertyOne = "some value" and wants the TextBox to change its text; the point is valid only if you are doing two way binding.
siz