This question already has answers here:
Wpf: Drag And Drop To A Textbox
(4 answers)
Closed 1 year ago.
In WPF, I need to drop a file to a combobox which is editable.
I can drop on the arrow and on the border of the combobox, but not directly on the main white part (the textbox part) I have tried to add handler for the sub-textbox, but no luck.
Here is the code I use for that (VB .net, I know...)
CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).AllowDrop = True
AddHandler CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).Drop, AddressOf Combo_Fichier1_Drop
AddHandler CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).DragEnter, AddressOf Function_DragEnter
AddHandler CType(Combo_Fichier1.Template.FindName("PART_EditableTextBox", Combo_Fichier1), TextBox).DragOver, AddressOf Function_DragEnter
My DragEnter function change the drag and drop effect to copy (working on the border and on the arrow) and my drop function simply set the text of the combobox with the name and path of the file. It's a sort of file selector with memory.
The problem likely occurs because the TextBox control supports the drag and drop of strings out of the box. Because of that it ignores other objects.
It is possible to use the PreviewDragOver event to avoid this problem as described in this thread: https://stackoverflow.com/a/23150582/12797700
Related
I got a pretty complex WPF-Window with a lot of sub controls using strict MVVM. One of them is an ItemsControl. What I want to accomplish is to propagate a PropertyChanged-Event to Items within the ItemsControl when the Alt-Key is pressed anywhere - not just when the ItemsControl is focused. How can I do that?
An example: when I press the LeftAlt-Key anywhere within the Main Window I want to change the text of a button which is part of an item within an ItemsControl.
see similar question here
answer with more than 100 upvotes
or consider using a Behavior with command
Edit :
Behavior on the upper control(MainView i guess) to register to the KeyDown Event and excute a command on your ViewModel
I have a form in my project that has the controls created through use of a class. There's an issue with this way of it though, since any of the controls I will create will be under an ItemsControl control, I'm not quite sure how I would access the events of any of the controls I create(textbox for example)
If I want to create a KeyPress event for my textbox, I have an error in the code:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
I'm only assuming this is because of my ItemsControl because I've ran into this same issue when trying to access controls' properties from within the ItemsControl and it was a nightmare to fix. What's the real reason this is throwing an error?
Your problem is that you're trying to put this functionality in the wrong place.
The code behind the Window that contains your ItemsControl is not the right place to put code which affects / is concerned about a particular UI element which is reused over and over in an ItemsControl. You need to isolate the "pieces" of your UI and create modular, individual units of functionality / Visuals that can be reused and work independently of their "surrounding" environment.
In this case, you have to replace the TextBox in your DataTemplate (in XAML) for another UI element which performs like a TextBox, but only allows numeric / masked input.
There are 3 different approaches to that:
1 - replace the TextBox for a MaskedTextBox from the WPF toolkit.
2 - replace the TextBox for a UserControl that contains the TextBox and uses code behind to perform the numeric-only input functionality.
3- replace the TextBox for a Custom Control that inherits from TextBox and performs the input validation in on it's own.
The difference between approaches 2 and 3 is that a UserControl is a composite piece of UI made up of XAML + Code Behind, which can contain any other UI elements, whereas a Custom Control is a code-only (no XAML / "Lookless") class that is derived from a Control (such as TextBox) which requires a ControlTemplate to function properly.
Notice that neither of these approaches involve putting code behind the Window that contains your itemsControl. It is simply a matter of separation of concerns and proper encapsulation of functionality.
You need to subscribe to the event dynamically, using the AddHandler keyword:
Dim tb As New TextBox
...
AddHandler tb.TextChanged, AddressOf tb_TextChanged
where tb_TextChanged is the method that handles the event:
Private Sub tb_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
...
End Sub
If you need to unsubscribe, use the RemoveHandler keyword.
I am working on WPF datagrid control. Binding the data from code behind using a dataset/XML. I want to edit the cell of the grid and capture the new value which the user has entered. Is there a code sample to show how to do that?
What event should I use and which property of datagrid (if any) should I use, selecteditem or selectedcell?
(I am kind of new to WPF datagrid control, so I apologize if this is kind of kiddish...)
You can use CellEditEnding and RowEditEnding. You can access the respective row and its Item from the event arguments.
I want to catch the event when a listview is left-clicked on an empty space - i.e. clicking on no item within the listview control.
I've search in the Events list of the listview but found none. How can I do this?
Please help!
[Edit]
What I want to do if I could catch this event: Deselect all items in the listview.
If you attach a handler to the MouseLeftButtonDown event on the ListView it will only fire when areas outside of a ListViewItem are clicked. Any clicks inside the items will be handled by the items themselves to drive the ListView's selection behavior.
You can make changes to the clickable areas by adjusting the Background ({x:Null} is not clickable, anything else is) and Margin of the ListViewItems by setting an ItemContainerStyle on the ListView. Also make sure that you are not using a null Background on the ListView itself (White is the default, Transparent works too).
ListBoxItem control handles clicks on ListBox. You should either:
use PreviewMouseDown event on ListBox
Add event handler in code via myListBox.AddHandler method
See How to Attach to MouseDown Event on ListBox for explanation and code examples.
I found if I had previously single clicked on an item in the listview (and thereby selecting it), then next double clicked on the empty space in the listview the undesired result of having the previously selected item being actioned as if it had been double clicked on (rather then the empty space). To get around this I used the following code (vb.net):
Private Sub ListView1_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles ListView1.MouseLeftButtonDown
ListView1.SelectedIndex = -1
End Sub
with this code in place double clicking on the empty space de-selects any previously selected items and has the desired effect of nothing appearing to happen for the user when they double click in an empty area.
To give the ability to display and edit Richtext (Xaml) inside a treeview item,I have used a bindable Richtextbox (custom user control that used a richtextbox) to the HierachicalDataTemplate that used to populate the TreeViewItems.
This works fine but at the same time I want to have the drag and grop functionality in the treeview. My problem is when I click on the richtextbox (inside the treeviewitem) and do a drag it will do the richtextbox content dragging. What I want is to drag the treeviewitem insted (fire the drag and drop events of the treeviewitem).
NOTE: If I used a textblock this can be done but can't display richtext. Any Help?
Trigger your drag from the on the "Preview" mouse events rather than the regular ones, and set the Handled flag in PreviewMouseMove when doing a drag so the RichTextBox never sees the MouseMove.
For example, you might record the mouse down location in PreviewMouseLeftButtonDown, then if you receive a PreviewMouseMove with the left button still down you can set e.Handled=true and then call DoDragDrop in a dispatcher callback.
The reason this works is that RichTextBox uses the regular (non-preview) mouse events.