How to determine when a WPF User Control has completed loading
Asked Answered
M

2

6

I have a WPF User Control with a UIElement Grid with several text boxes and combo boxes. I have a Button(btnApply) whose IsEnabled state should be false when the form has finished loading. However, the TextChanged event is being fired as the grid is being populated and changes the IsEnabled to true. I've added a boolean method that does keep the btnApply.IsEnabled = false after the user control has completed loading. But, I can't tell when the User Control has fully completed loading to alter the boolean state of my method to allow the btnApply.IsEnabled to be altered. I've tried Loaded events on the User Control and tried checking the IsLoaded event and they are all fired before all the elements on the User Control have finished loading.

Edit: The User Control's UIElement Grid is populated via ItemsSource = List. The text box and combo box changes are trapped via TextChanged and SelectionChanged events.

Mortar answered 25/7, 2011 at 18:53 Comment(1)
What is triggering the TextChanged event?Entropy
A
6

Try using the Dispatcher to fire the IsEnabled change at a later DispatcherPriority than Loaded, such as Input.

Dispatcher.BeginInvoke(DispatcherPriority.Input,
    new Action(delegate() { btnApply.IsEnabled = false; } ));

You can view the DispatcherPriority order here

Ankh answered 25/7, 2011 at 19:11 Comment(4)
Rachel, I like this idea. I think what it implies is that I can change the IsEnabled property to only be fired after the User Control has finished loading, i.e enumeration value 5 (input). Only, it doesn't work. Here is my delegate - public delegate void MyApplyDelegate(); and I added this just after my InitializeComponent(); btnApply.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(delegate() { btnApply.IsEnabled = false; }));Mortar
@Mortar What is occurring during your control's Load process that takes so long? Are you sure the issue is that the Loaded event isn't finished yet? Perhaps you can post more of your code.Ankh
I got it working made the following change btnApply.Dispatcher.BeginInvoke(DispatcherPriority.Input, new MyApplyDelegate(delegate() { btnApply.IsEnabled = false; }));Mortar
Also found this to be more helpful because it included the use of the delegate.Mortar
F
0

Maybe I misunderstood the problem, but registering for Initialized / Loaded on the UserControl does not do the trick? If not, how do you populate the Grid?

Fitzgerald answered 25/7, 2011 at 19:42 Comment(1)
Correct Initialized and Loaded trigger prior to the grid being completely populated. I have a TextChanged event that was being triggered as the grid loaded. However, I placed a Boolean method that stops the TextChanged event. Now I don't know exactly when the User Control and the Grid have completely finished loading in order to turn my method off(i.e. TRUE) so the TextChanged event will know it's okay to fire. Granted my method doesn't need to be a part of the solution. It is only set as a way to keep the default state of btnApply.IsEnable equaling false.Mortar

© 2022 - 2024 — McMap. All rights reserved.