I have a UserControl in my WinForms project. I add some objects of this UserControl to a FlowLayoutPanel at run time using code.
I want when I add my first UserControl to FlowLayoutPanel, change its BackColor.
Is there any event for the UserControl to aware when add it to a parent control(something like UserControlAddedToParent)?
it seems that you are looking for Control.ParentChanged event
This event is raised if the Parent property is changed by either a programmatic modification or user interaction
Related
I have controls in the same place (one on top of the other)
Is there a way to get an event when the z-order changes?
The purpose is for debugging to see when and who change the order
It can be changed by BringToFront or SendToBack or SetChildIndex
Like Control.ZOrderChanged or form.Controls.ZOrderChanged
Changing the ChildIndex will trigger a Layout Event on the control that is parent to the child control. Of course this assumes that SuspendLayout has not been called on the parent control.
You can filter the event by checking the LayoutEventArgs.AffectedProperty Property (a string) to see if it is equal to "ChildIndex". To determine which control triggered the event, check the LayoutEventArgs.AffectedControl Property
I'm writing an application that is creating most of the WPF interface with C#. When the user double-clicks a node in a tree view, an instance is created of a class I designed.
That class handles everything else - creating a ListView control and a GridView, setting the ListView view to the GridView, and finally creating a new TabItem, populating it with the ListView, and adding the TabItem to a pre-existing TabControl.
I'm running into problems adding a GridViewColumn.Click event handler to my ListView, because I want the function for the click event to be in my main window class, not the separate class that creates the ListView and GridView.
I've figured out how to use ref to reference the TabControl in my WPF window from my separate class, but what about functions? How can I add a click event handler in my class that references a function in the main window class?
I would use a command instead of a event handler. Then you can bind the command to whichever class you want to handle it
check Bind event to ViewModel
I have a long form with a bunch of CheckBox's and occasionally some TextBox inputs. I want an event to be raised any time any control is changed (i.e., any CheckBox state is changed or any TextBox.Text is altered). Is there a global way to do this without having to add an event handler to each and every control?
One of the advantages of WPF and its declarative nature is that events are inherited down the visual tree.
<Window x:Class="MyApplication.MainWindow"
....
TextBox.TextChanged="TextBox_TextChanged" CheckBox.Checked="CheckBox_Checked">
All TextBox and CheckBox controls will inherit these event handlers. The same approach can be taken in other controls such as Grid so only the controls within the Grid are affected.
One way do this would be to subclass the CheckBox and TextBox classes and implement the required handlers in those classes.
You will still need to go through your application and replace the standard CheckBox and TextBox with your classes.
You can use TextBoxBase.TextChanged="MainWindow_OnTextChanged" on window or user control.
There are so called class events in WPF. Put this in the form's constructor:
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.TextChangedEvent,
new RoutedEventHandler(AnyTextBox_OnTextChanged));
This way you register a handler to TextChanged event of all text boxes. Similarly with check boxes:
EventManager.RegisterClassHandler(typeof(CheckBox), CheckBox.CheckedEvent,
new RoutedEventHandler(AnyCheckBox_OnChecked));
I have a Canvas with different Elements which are created and removed dynamically at runtime inside a canvas, which itself is in a custom Usercontrol.
Now I want to add a few Touchevents, like ManipulationStarted. However, if I bind it on the Canvas, I get the canvas back as sender in my event. I need, however, the specific UIElement that was touched, so I guess I need to bind the event directly to the children. How can I achieve this?
PS: I've already tried creating a custom Event inside the Usercontrol, which is added to the specific child when it's created, however the performance seems to suffer immensely from this approach.
I have FlowLayoutPanel and UserControl's on it with drag & drop reordering. This sort of works. But the problem is that child controls prevent dragging of the actual parent UserControl.
So my question is how to enable dragging of a UserControl that contains child controls?
If I understand you right I had the same problem as you and I solved it by propagating events of the child element to it's parent.
If you have a draggable UserControl containing a label. You have to call the events of the UserControl when the events of the label occurs. E.g. in the Label's OnMouseDown() call the UserControl's OnMouseDown() and just pass the Event-Args. I didn't find a better way than handling each event that is required for drag and drop separately.