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
Related
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
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 form with a lot of user input controls; most of them are optional, and for reasons beyond my control the required elements are scattered around the form. I've been asked to add a button that opens a second form (henceforth called ChildForm) which is linked to the original form (henceforth called ParentForm) and has only the required controls from ParentForm.
I want the controls in ChildForm to be linked to the same datasource as their corresponding controls in ParentForm. I'd like to create this linkage programmatically in a loop so that later changes to ParentForm don't require manually editing the databindings of ChildForm controls.
I tried ChildControl.DataBindings.Add(ParentControl.DataBindings[0]); but I get a dataBinding belongs to another BindingsCollection ArgumentException at runtime.
How can I bind a new control to the same column of a DataTable as an existing control without manually doing it for each control?
If your Binding is simple (doesn't have any Format and Parse event handler registered), you can do a shallow clone like this:
public void CloneBinding(Control control, Binding bind){
Binding bind = new Binding(bind.PropertyName, bind.DataSource, bind.BindingMemberInfo.BindingMember);
control.DataBindings.Add(bind);
}
//Use it
CloneBinding(ChildControl, ParentControl.DataBindings[0]);
I have some problems trying to update components of surface elements. I dont know if my approach to the problem is wrong, since I'm new to the topic.
My point is represented by the following diagram
According to the option that is selected in the menu, load different user controls as only child of StackPanel but i'm habing problems for update the Listview from loaded user controls, example: when I save a new item I need to recharge the list of items in the ListView
MVVM would be a good pattern here. If you have a problem passing data between controls - why not introduce them on top of unified data layer? Consider this:
Three radio buttons in your Menu, each one's IsChecked property bound to Visibility property of your respective UserControl.
StackPanel holding all three UserControls
ListView bound to ViewModel's List<Item>
Each of your UserControls bound to ListView.SelectedItem: one of them using TextBlock for read-only, one using TextBox for editing. Third one would create new item in your List<Item>. You would have to create ItemTemplate for each or create one UserControl (since they look very much alike) and use DataTemplateSelector.
If you're not familiar with MVVM here is a good start. You can also use one of the existing frameworks like MVVM Light
You can create an event on your child
public delegate void HandleNAMEOFYOURHANDLEEVENT();
on your child class
public event HandleNAMEOFYOURHANDLEEVENT yourInstance;
to use it on your child class
if (!ReferenceEquals(yourInstance, null))
{
yourInstance();
}
and you declare it on your parent like other event.
I have a windows form that contains two usercontrols. One usercontrol contains two list views. The other usercontrol has a grid.
Within the first usercontrol the two list views drag and drop their contents between each other.
The grid usercontrol is setup to dragdrop onto the listview usercontrol.
The problem is that the drag drop event within the listview usercontrol always takes precedence over the dragdrop between the two usercontrols.
So if I drag from the grid usercontrol over the listview usercontrol it will execute the internal dragdrop event of the listview control.
In other words it fires this event
lv_groupActivites_DragDrop
instead of
reservationScheduleBooking1_DragDrop
Is there anyway of specifying which drag drop event should be fired?
You are dragging from the grid in one user control and trying to drop onto a specific listview on the other user control or anywhere on the other user control?
If you want to drop anywhere on the other user control, I think you will need to setup the apprporate event handler on that user control to respond. If you want to drop on to a specific listview that also accepts drag events from the other listview you will need to do extra work in your event handler on that listview to figure out where the drag initiated from.