Before Binding event? - c#

Is there an event that fires before the window has loaded and before any bindings take place?

You can use constructor of the Window to run code before window loaded and bindings take place. However if you need an event Initialized event of the window will work for you.
public MainWindow()
{
// Here it is...
InitializeComponent();
}

You don't have specific event but you can nevertheless capture the event of change of the value of control.
For example for a TextBox, you can subscribe to OnTextChanged.
He is executed before update source.

You can try the Window.Initialized event.

Related

Is there any case in which the Form's Activated event is not raised?

I don't understand why it could be that, I thought Activated should be raised when the form is shown. In fact my form has TopLevel set to false and it's added to another form. When the main form shows, it's also visible, and I can interact with its controls but I tested and saw that the Activated is not raised.
public MainForm(){
InitializeComponent();
Form child = new Form();
child.Activated += (s,e) => {
MessageBox.Show("Activated!");
};
child.Size = new Size(200,100);
child.TopLevel = false;
child.Show();
child.Parent = this;
}
After running the MainForm the child form is appeared inside the main one and there isn't any MessageBox displayed with the message "Activated!".
What is the additional job to do to make it raise?
If the second form comes to screen for the first time, you can use Shown event.
Activate event is only fired when a form gets focus, but that does not contain showing for the first time. But, if the previous form which is active is outside of your app, it will not raise activate event. I mean it is valid when only viewing forms of same project.
Here is my answer, I noticed that only Form has Activated event, other controls don't have and once the TopLevel of Form is set to false, I think it's treated as a normal control and in that case, Activate() method will do nothing and Activated event won't be raised in any case. I think this is the reason why Activated is not raised.
Thank Kuzgun for a suggestion of using Shown instead, but this is focused on explaining why the Activated is not raised!
This answer is just my guess, even the MSDN page about Form.Activated event doesn't mention this. It should not be missed that way especially in an official documentation page.
Once the TopLevel property of Form is set to false then the form becomes a normal control, hence Activated() event will not fire.

Raise event in a User Control to MainWindow

Sorry first, because I see another question, but both of the ans. and ques. is not clear enough
How can I raise a parent event from a user control in WPF?
In my MainWindow.xaml, I had a right Panel
<local:RightSideContent x:Name="RightPanel" Grid.Column="1">
So in the MainWindow.xaml.cs, if I want rise an event to this panel, I made the code like this:
public delegate void Event1();
public MainWindow()
{
this.InitializeComponent();
Event1 obj = new Event1(this.RightPanel.func);
obj();
// Insert code required on object creation below this point.
}
And in RightPanel class, I declare the function func
The question is: if I am in the RightPanel Class, how I raise an event to the MainWindow, because I can't wrote something like this.RightPanel.func.....
And by the way I am in another class that do not have xaml file, if I want raise an event to a UserControl, how can I do?
Sorry, I don't quite have enough rep to post a comment to clarify, but as I see it, there are three possible things you are trying to do here.
You are trying to trigger an event on MainWindow, from some code that doesn't reside in MainWindow. In which case, you need to make sure that you have a reference to MainWindow, and that there is a public method on MainWindow that will trigger that event.
You want MainWindow to handle a click etc that comes from RightPanel. In that case you simply put a Button.Click="blah" (or whatever the event is) attribute on your MainWindow, and it will catch any button clicks from below it that are not handled lower down. In fact you can even handle it lower down and make sure that you set the EventArgs so that it is effectively unhandled, so that you can then handle it higher up as well.
You want to be able to handle a custom event generated in RightPanel, in a similar way to the way you would the button click scenario from item 2 above. In this case, I would direct you to http://msdn.microsoft.com/en-us/library/ms742806.aspx which is the documentation for Routed Events in WPF, and you should be able to work out how to create your own RoutedEvent from there.

Registering MouseDown and MouseMove on Form

How and where can I register a mouse event on a form. When I doubleclick on the form it'll generate the Form_Load event for me and I can add code into there. However when I add something like
private void Form1_MouseDown(object sender, MouseEventArgs e{
Console.WriteLine("mouse down")
}
However when I do a mousedown event on the form I don't get anything on the console. I know something is missing where I register the event to the form or something of the sort. Any ideas?
Thanks,
In the designer view, select the form and then in the properties window, click the little lightning bolt (events).
Here you're able to select which delegate method is called for which event. If you haven't created the method already, just double click the empty space next to an event and it will generate the code for you.
If you are using VS.net then you should find all the events in the property panel. Just pick the ones you want.
If you want to grammatically register an event then the code would looks like:
Form1.Click += new MouseEventHandler(Form1_MouseDown);
in order to unregister it's
Form1.Click -= new MouseEventHandler(Form1_MouseDown);
The event needs to be "wired up" either from the designer or from code. You can wire up an event from Visual Studio by double-clicking the event in the properties window:
Which generates code like the following in the auto-generated .designer file:
theForm.MouseDown += new MouseEventHandler(Form1_MouseDown);
You can also use the code like the above to manually wire up events in your Form_Load method.
With WinForms, you'll want to add it via the design view in Visual Studio.
While in the design view, select your form. Then, click on the 'Events' button in the Properties panel (looks like a lightning bolt) and type in the function name under the appropriate event. You can also click on an event here to automatically generate a new function in the code-behind.
As you noticed, double-clicking the form will automatically generate a certain function in the code-behind. For forms, it is Load but for other things it may be MouseDown or some other event.
By your explanation it seems like you have registered the event properly through the designer... if you still don't see the string on the console try System.Diagnostics.Debug.WriteLine (Maybe you are looking on the wrong window)

wpf combo change event triggered when form loads

I am having a problem, when window loads, the 'selection_change' event associated with 'combo box' control is getting triggered when the window loads first time. Why its occuring and How to restrict it please?
Regards
With that code, the SelectionChanged event won't get raised. Create a new project, paste it and try it for yourself.
My guess is pretty much the same as Sekhar_ Pro's, you're populating your ComboBox from code behind, and something in there causes the SelectedItem to change.
Investigate the cmbUsers.SelectedItem in the cmbUsers_SelectionChanged event handler to see if it has some value or is null in the debugger. Also, look in the Call Stack to find what caused this event to be raised.
Example code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
cmbUsers.Items.Add(new ComboBoxItem { Content = "Test" });
cmbUsers.SelectedIndex = 0;
}
private void cmbUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cmbUsers.SelectedItem != null)
{
MessageBox.Show(cmbUsers.SelectedItem.ToString());
}
}
}
The Call Stack looks like this for me in the event handler
This is not a normal Behavior, some where you must be doing something like setting SelectedItem, etc which in turn is triggering the event. Check thorough your form's life-cycle events and see if you are doing something like this, may be in Load or Activate event or somewhere in the Constructors.

C# WPF which method is called when I call a window's show()?

I have window A and window B. In window A I call B.show(). I want to know in window B which method is called and I want to load data when B is showing up. thanks,
You can always listen to the "Loaded" event:
BWindow.Loaded += new RoutedEventHandler(BWindow_Loaded);
void BWindow_Loaded(object sender, RoutedEventArgs e)
{
//Your Code here
}
Then in your AWindow call
BWindow.Show();
You may be surprized, but when you call B.Show(), the method which is called is Show().
About loading additional data after window B is shown, you may subscribe to its Loaded event (see answer of #masenkablast). The better idea would be perhaps to derive from Window class and bind to the needed data in XAML. (You are using WPF, not WinForms, I suppose.)
I think it is better to have a Property which is set before B.Show() is called. This way you can always get the method which invokes the show method based on the property.

Categories