I'm adding manually a click event in main window of mainwindow.xaml. There are no faults in xaml editor and the button is visible.
When switching back to the mainwindow.xaml.cs my click event is NOT automatically added. It goes for all events. These events should be automatically added in the .cs editor along the lines of:
private void Button_Click(object sender, RoutedEventsArg e)
{
}
Any ideas, pointers?
If by "adding manually an click event" you mean manually typing in the XAML in the editor then no it does not add it for you at that point.
If IntelliSense is working for you, as you type in the Click="" text, it should popup a menu listing the already available click handlers that you can select AND at the top of that list should be a choice labelled "<New Event Handler>". If you select "<New Event Handler>" it will add it in for you at that time.
If you don't choose anything from the list but instead just type it in, it is not added. However, at that point, at least for Button controls, you can double-click the button control in the XAML Designer and it will add the click handler into the .xaml.cs code and take you to it.
Related
This has me baffled.
I've written a popup box which consists of a WinForms UserControl hosted inside a WindowsFormsHost, which, in turn, is hosted in a Primitives.Popup which is displayed on the screen. The whole application is WPF, but this control was lifted from an earlier application written in WinForms.
The popup is activated by an external event (an incoming phone call from a CTI Server).
Inside the UserControl is a textbox control. When the user clicks in the text box, I call the Focus method on the Popup, then call the Focus method on the textbox. The textbox gets the focus. I can be fairly sure of that because the box shows a cursor after clicking in it, and also I have a "GotFocus" event handler that prints a debugging message.
However, if there was another program active at the time the incoming event occurs, any keys that are pressed on the keyboard continue to go to that program, not to the text box. Only if the user clicks in another part of my application (i.e., part of the screen outside the popup) to make it the active program, and then clicks in the text box is the text box able to receive keyboard input.
I hope I've given enough information without overwhelming you with the myriad of details. If there's something else anyone needs to point me in the right direction, I'll be happy to provide it.
Because the WinForm TextBox is hosted, setting focus on it does not activate the hosting WPF Window. Add a line to activate the Window.
private void TextBox_Click(object sender, EventArgs e)
{
this.Activate(); //activate the Window
(sender as System.Windows.Forms.TextBox).Focus();
}
When I double click on my User Control file, system auto generates a UserControl_Loaded function in .cs file and adds loaded tag in XAML as Loaded="UserControl_Loaded" for UserControl element. It also defines an eventHandler to this.Any shortcut in VS to undo this operation quickly?
You can just Undo once in the .xaml.cs and then flip back to the .xaml and Undo once there and you'll be back to the initial state. Quick keyboard sequence is Ctrl-Z, Ctrl-Tab, Ctrl-Z.
I have a solution, that has two projects, the main one and a small shared control that's used as well. In our application, a certain feature opens this shared control in a new window. I want to set the focus to the first combobox in this control when the window opens.
In my code, on the window that loads the shared control, at the end of the _Loaded event, I set focus to this combobox. But when running the code - I still have to hit tab to have 'keyboard' focus on the box (as in, I would have to hit tab to then start typing the name of one of the items in the list).
If I set a breakpoint here, hit it, and then continue - it actually is set the way it should be. If I use a WPF inspector - IsFocused is also set.
Other things noticed:
If I hit tab (to get what I want), then tab back, it takes me to the last control on the form, not to this unknown control. This makes me believe the focus is set right, but for some reason doesn't have correct keyboard focus.
If I try to use MoveNext in code, it actually selects the next item in the window, outside of the control.
How do I properly set focus here? On another combobox in the 'main' project, just calling .Focus() worked correctly.
try to postpone the focus() after all events are handled and bindings updated with QueueUserWorkItem. Something like this :
public delegate void VoidDelegate();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Some other things to do here.
System.Threading.ThreadPool.QueueUserWorkItem
(x => this.Dispatcher.Invoke(
new VoidDelegate(SetFocus), null));
}
private void SetFocus()
{
MyControlIWantToSetFocusOn.Focus();
}
I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.
So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.
The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.
How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?
Tony
You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.
private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
textBox1.Text = ((FrameworkElement)e.NewFocus).Name ;
}
Try to press Tab Key and see if it helps you find the control in focus.
You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?
For starters, Snoop shows the current focused element and the current
FocusScope in the status bar.
You can get it to show you all the GotFocus and LostFocus events:
1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.
Similarly you can track the FocusManager events the same way.
I have a WPF ListBox, where the items are styled to be Buttons. I've used gong-wpf-dragdrop to very easily implement drag-and-drop, so I can reorder the listitems. However, I would now like to be able to left-click on one of the buttons to launch an edit action, but the MouseLeftButtonUp event on the ListBox seems to be being swallowed by the drag-and-drop operation. (I'm using EventToCommand from MVVM Light to hook everything up).
Changing the ListBox to respond to MouseRightButtonUp works fine (so I can drag-and-drop with the left mouse button, and launch the edit action with the right mouse button), but I would rather keep the right mouse button for a context menu.
I also tried using MouseDoubleClick, but although that launched the edit action, it always opened the first item in the list for editing, and moved the listitem that was double-clicked to the top of the list - very confusing for everyone concerned!
Any thoughts on how to approach this?
If i understand you correctly, you don't want to drag&drop if you click the button. One way to do that is to hook up with the PreviewMouseLeftButtonDown of the button. Initiate you edit action in the eventhandler and set e.Handled=true. This way the MouseLeftButtonDown will not arrive at the listbox and no drag&drop operation will be initiated. One disadvantage is that the button will react on the mousedown instead of the mouseup (so it's like ClickMode=Press).
Regards,
Rob