Updating a PropertyGrid - c#

How can I have a property grid update automatically when the object in its SelectedObject property changes? I've tried implementing INotifyPropertyChanged in my class but the property grid does not actually show the new propertyies of the object in the background until I click on it.
I've tried subscribing to the PropertyChanged event of my object directly, and calling the Refresh() method of the PropertyGrid when it is envoked. But some of my properties are related. Meaning changing one property may evoke multiple PropertyChanged events. This seems to work fine, but I'm still wondering if there is a cleaner way of doing this through DataBinding. Also I'd like to avoid having the control Refresh multiple times after the user only updated a single property.
So is there a way to get the PropertyGrid to refresh from PropertyChanged events?

Try adding the RefreshProperties attribute to each property that has dependencies:
[RefreshProperties(RefreshProperties.All)]
Now, each time this property changes - it will automatically refresh the other fields.
This is a much cleaner approach than calling the property-grid's "Refresh()" each time.

I don't know if there's a built-in way to do it, but here's a suggestion if you want to avoid multiple calls to Refresh for related properties :
When a PropertyChanged event occurs, start a timer. If the event occurs again before the timer has elapsed, do nothing. In the Tick event of the timer, refresh the PropertyGrid and stop the timer

Related

Property Changed event within class

I got a question about events within classes.
I have a whole load of properties within my class and want to fire an event whenever a property is changed. I want to use the changed properties to calculate a result.
One possible option might be adding a call to every single property-set but this is - for my understanding - too q'n'd.
The common approach to realise this would be to implement the INotitfyPropertyChanged interface and raise the PropertyChanged event in every settter method for the properties where you need it.
Then you implement an event handler which reacts to the event and performs the operations needed.
https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

WPF Validate/Setter calls in wrong order

I have a textbox inside a TabControl. The textbox binding has UpdateSourceTrigger=LostFocus. The textbox uses Attribute based validation from the data model. This validation is working correctly.
In the TabControl.SelectedItemChanged event I call modelObject.Validate() and prevent the switch to a different tab if an error occurs.
The problem I have is that the order of execution is backwards. The validate call occurs before the property setter. In the case of an invalid field I am able to switch away from the tab even though an error has been detected.
How do I get the order or these events ordered properly?
Is there a way to cancel TabControl.Items.CurrentChanging?
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d8ac2677-b760-4388-a797-b39db84a7e0f/how-to-cancel-tabcontrolselectionchanged?forum=wpf
Seems to work by subscribing the CurrentChanging event. Can cancel tab changing action by setting the CurrentChangingEventArgs Cancel to true
WPF is definitely incorrect here. However, the fix is really simple.
In the SelectionChanged event handler call {whatever your tabcontrol is called}.Focus(). This immediately forces the lostfocus event for the textbox (which forces the setter to be fired) and solves the problem.

Differenciating selection change depending on call from code or changed by hand

I would like to know if it is possible to make a difference in the event selection change of a combobox.
I want to make a difference between a user that manually click the combobox and changes its value and a change in the selection that i do from code.
ie :
if i click my combobox and change its value by hand, the event is fired
but if i do myCombobox.selectedItem=1 [edit] the events is not fired
Is there an event that has this behaviour in a wpfcombobox ?
If not, do you have any idea on how to do that ?
Thanks
[edit] or if it is the binding of the combobox that changes its value
You are dealing with a couple of different scenarios, both of which are solvable.
1) Don't process SelectedItem requests during databinding. You have at least two options here:
a) Don't add the event handlers to the control until after databinding is complete or the form is loaded (depending on whether or not databinding is automatic or manual).
b) Set a form level property indicating when it is ok to process the SelectedItem event. You will probably want to set this to true after the form is loaded or after the databinding is complete. In your SelectedItem code, don't perform any actions unless this property is true.
2) Process the SelectedItem logic if the SelectedItem is changed programatically. Again, two options:
a) Extract your logic from the SelectedItem event into a method and then call this method when you perform the logic to set the selected item.
b) Create a custom combobox that inherits from the base and add a SetSelectedItem method (for example) to this inherited combo. This method would then raise the SelectedItem method. This would be reusable and you wouldn't have to remember to do 2 pieces of work whenever you set the SelectedItem manually.
Before you change the selecteditem in code, remove the event handler with -=, then add it back afterwards.

How to bind a WPF control value to a method?

Imagine I have a TextBox that it's Text should be equal to the number of running processes in the machine.
How to make it to update without using timers? Is there a way using Dependency Property or Bindings?
I'm not sure what this has to with binding to a method.
You'd have to keep calling the method on a timer, just the same.
You can set the DataContext to an object that implements INotifyPropertyChanged and exposes a property with the data you want (the getter can, of course, call a method).
Without a timer, you'll need an event to which your data object can subscribe, and then you can raise a notification that the property of interest has changed each time the event fires, and the UI element bound to that property will be updated.

Event Tracking in C#

I want to understand event cycles. I have a form with a grid and textboxes. It has a grid, bound to DataTable, and textboxes bound to same table too. I'm trying to debug something and need to know how to identify ALL events fired in the form to see what may solve an issue for me.
Anyhow, unless I explicitly subclass every class on my form, and override / attach to every event to my own event handlers, how can I get / listen to all events being fired during a certain action... Such as changing a "Selected" road in a DataGridView. It obviously updates its own "CurrentRow"... I need to know what / how to maybe FORCE a re-loading of SAME CurrentRow.
Reason: during a form level "Edit Mode", and I change the content in another "Textbox" control, and reject changes, I need it to simulate the current "Record" is reloaded to go back to its original values.
You could fire up a profiler and look at the method call tree.

Categories