Is there any event before source is updated in binding in WPF? - wpf

I am looking for something that fire before the source is update
So instead of Binding.SourceUpdated I want Binding.PreviewSourceUpdated

I think you would need to handle this on your UI element and intercept the event if it's not ideal. So for example say you had a text box and a user pressed a key. You would use the PreviewTextInput (or similar preview) to see if you wanted the data changed - then mark the event has handled if you didn't.
The only other option that I know of is to use DataValidation and not allow the property to be updated for the model side.

Related

C# "Textchanged" event and infinite loop?

I'm currently creating an application that has three textboxes. Typing into one box will result in the other two having "converted" versions of the text displayed. All three boxes can be typed into and serve the same purpose (though provide different outputs for the conversion).
The "TextChanged" event is called whenever the text value of the box is changed, that is fairly self-explanatory. But will this event also trigger if I change the value through code.
Say if I changed the first box, it would create text in the second. Would the second box trigger the event as well? And would this result in an infinite loop?
No, it wouldn't. Text taken from the MSDN page on the event:
The TextChanged event is raised when the content of the text box changes between posts to the server. The event is only raised if the text is changed by the user; the event is not raised if the text is changed programmatically.
MSDN page
Yes, or no.
this behavior differs from Winform and ASP.Net (each corresponding to System.Windows.Forms.TextBox and System.Web.UI.WebControls.TextBox), means that if you are to create ASP.Net application then the answer is no, but if you are working on Winform application then the answer is yes.
Please refer to the following different saying from the MSDN:
System.Windows.Forms.Control.TextChanged Event
Remarks
This event is raised if the Text property is changed by either a
programmatic modification or user interaction.
For more information about handling events, see Handling and Raising
Events.
System.Web.UI.WebControls.TextBox.TextChanged Event
The TextChanged event is raised when the content of the text box
changes between posts to the server. The event is only raised if the
text is changed by the user; the event is not raised if the text is
changed programmatically.

Google realtime API OBJECT_CHANGED event fired by (almost) anything happening?

Take a custom realtime object "MyObject" one property of which is an IndexReference referring to a CollaborativeList "MyList". "MyObject" also has a collaborative field property "MyProps" into which I put a json string.
In order to detect changes to "MyProps" I tried the following:
MyObject.addEventListener(gapi.drive.realtime.EventType.OBJECT_CHANGED, dealWithChanges);
However, when I make any change to an object in "MyList", which shouldn't affect the IndexReference in any way, the OBJECT_CHANGED event is fired on "MyObject"
Is this by design? It makes the OBJECT_CHANGED event useless, effectively a "something has changed somewhere" event, but without it there appears no way to detect a change to "MyProps".
Object changed events bubble, so you can listen higher up the tree for anything that is happening lower.
To listen only for changes on collaborative fields within the particular custom object, you want a ValueChangedEvent

AutoCompleteBox: do not validate with Up/Down keys

My AutoCompleteBox calls a WCF service when the selection is changed, usually with the mouse. However if the user uses the arrow keys to navigate through the selection, the event fires up for each element, making the application too much data intensive.
How do I prevent the AutoCompleteBox_SelectionChanged to fire when the keys are pressed?
I found this which sounded like a nice solution but it doesn't work http://betaforums.silverlight.net/forums/p/137710/307786.aspx
ok, rather than using AutoCompleteBox_SelectionChanged, I'm using AutoCompleteBox_DropDownClosed, and this fixed the problem.
I just found how I have solved this same problem. Also I am not using SelectionChanged.
I added behavior to item DataTemplate (to Grid root). This behavior attach click handler to item. When keys are used, behavior is "sleeping", when I click on item with mouse, behavior get called and make its work. (Also I bind needed property of item to a DataTemplate's Grid's Tag property, so I can get to it from behavior)
Not suitable for every solution, but can be useful.

Events routing in WPF

I have implemented a UserControl. Then I would like to handle an event that is originally handled by Window (keyboard press). What is the best way to route the event caught by another component (higher in the components' tree)?
Thanks in advance for the replies and hints!
Cheers
It depends on the event you're trying to access. If it's a Preview event and the Window is setting e.Handled to true you'll need to use the method Alex suggests to circumvent the Window's handling of the tunneling. If it is a bubbling event (i.e. KeyDown) you don't need to do anything special since bubbling events hit the handlers on child elements first and go up the visual tree so the Window handler won't occur until after your UC's.
One thing you need to be careful with using Key events is that the event is only going to get picked up by your UC in the first place if the Focus is on or inside of it. This isn't something you need to worry about with things like Mouse events since they start at a specific location in the tree.
I believe you cannot gurantee that.
Window class is wrapping Win32 message-based event model and this will be the only WPF entity which will have access to those information.
I suggest that you create an attached property (which will be used by the Window) and implement the routing of the events yourself so that controls could subscribe to.
You can attach the routed handler specifying that you want to handle handled messages as well:
this.AddHandler(routedEvent, handler, true);
where this is an UIElement or derived class.
However there may still be events (key presses in this case) which don't make it past the window, not sure.

Distinguish UI change to variable from code-behind change

Like the title says I am trying to architecture into my application a way to distinguish the source of a variable change, either from UI or code-behind.
My problem is that I need to trigger some action after a property changed its value, but I only need to do this when the change comes from the UI because otherwise I don-t want to perform that action. I am having some trouble because, for example when a checkbox(two way binding), changes state, my binded property gets updated and then I use the checked and uncheked events to trigger that action.The problem is that when I change the property in codebehind it also triggers those events and I do not want that. Right now, i am using a flag that enables, or not, the actions at the event handlers but I do not feel that this is a good idea.
Any sugestions or ideas?
I am considering using only one-way binding and control everything my self, using commands.
It looks like you have some confusion between your model and your controller. There shouldn't be any cases where it matters if the a change to the model comes from the user or not. If you want to have something like a confirm message it the user makes the change, then don't bind the view control directly to the model, but have the controller respond to the event.
That way, if the control is changed to be the same as the model, then the change is internal, and no confirm is required, but if the control is changed by the user, then the control state differs from the model, and a confirm can be shown.

Resources