WPF ComboBox: click outside of popup keep popup open - c#

I'm using WPF ComboBox. When popup is opened and user clicks somewhere outside, popup closes. But I would like to leave popup opened and bypass standard behavior of the ComboBox in this case. I found here a solution. It is suitable for bypass the standard behavior of ComboBox, but the popup still closes. If I trying to set in this time ComboBox.IsDropDownOpen=true, my application crashes with error "Cannot reopen a popup in the closed event handler".
Have you any ideas how to solve this problem? Thanks!

I don't think that it is possible to set this behaviour to ComboBox. You can try to do that with ListBox though: just set ListBox.Height = 20 and expand it on ListBox.SelectionChanged().
And if for some reason you need ListBox to look exactly like ComboBox - you could use control templates.

Related

Stop displaying type cursor when clicked on a toolstrip

Im am making a toolstrip for my windows application in c#,
but when I press on a option of my toolstrip, the type cursor shows up.
It want it to be like a button, when you click on it something the code indicates happens. and not having the type cursor show up
Here is a photo of my problem:
I've already set ReadOnly to True, But this only prevents the user from typing in the toolstrip, not removing the type/text cursor.
I've tried Toolstrip.Cursor.Dispose(), But it gave me an error that toolstrip had no atribute Cursor. Al tough is should work on textboxes (haven't tried it on textboxes yet, because I have no need for that)
I'm a beginner with c#, but have some experience with python, so please go easy on me.
I expect that When I click on the box, The text/type cursor doesnt appear.
I use visual studio btw. I created the toolstrip in Design mode.
I Created A toolstrip, with a menu item, and In that menu item a textbox.
I shouldn't have created a textbox, but another menu item, and if that was pressed execute some code.
So If I just create a menu item in the toolstrip and create another menu item in the toolstrip, and set click to one of my code functions it acts as a button.

TextBox receiving focus when clicking on ListView

I'm struggling to make a custom autocomplete style control for my application using a list view. In one form, the custom control is working as expected, however in another, when clicking on the ListView, rather than the ListViewItem being selected, the first TextBox in the form is selected instead.
Rather than clog up the question with a wall of code, here is the code for the AutoCompleteListView.
Here is an image of the form. Items are redacted due to data protection:
Also, here is a .gif showing the behaviour of the form. What I'm doing in the aformentioned gif is attempting to click on the first item within the ListView, however, as I do that, focus changes to the first text box in the form. This same behaviour occurs when clicking on any item, in any of the 3 text boxes.
I have attempted to trace the events using RuntimeFlow to watch as events fire, but it has not provided any clarity as to what is actually happening. If anyone could point me in the right direction, that'd be greatly appreciated.
My first guess is in ItemClick you are setting visibility to false, which means something else in the form needs focus, ie the textbox. So do a textbox.focus() in ItemClick to the desired textbox. And given that you will not have access to the parent form in this class, I recommend you make an OnClose event in the AutoCompleteListView and subscribe to it in the parent form, to set the focus to somewhere.

Context menu not working on form only on objects

I all of a sudden can get my ContextMenuStrip to work on the form window itself. If i right click on my label or on my notify icon it works but nothing happens on the form itself. I'm certain it worked before I renamed the form but I can't seem to find out where the problem could stem from.
The ContextMenuStrip is correctly and identically set on both the form as well as the label and notify icon.
Anybody know where there problem could be? If my code is needed I will add it.
Check forms ContextMenuStrip property is properly set.

How to avoid losing the input focus on an HTML element inside a WebView?

I'm currently trying to build a touch keyboard to be used with an UWP app. One of the solutions I came up with was to use a brokered component to call SendKeys funcitons with buttons (IsTabStop being set to false and ClickMode to Press to prevent any loss of focus from the input controls). So far, everything works (tested it on TextBoxes without any trouble).
The problem: I'm encountering some issues using the WebView control : when I click a button, the WebView keeps the focus, as expected; however, the DOM elements (such as HTML's <input> markup) lose the input focus (while still being visually selected).
I found a way to solve this by resetting the focus on the webview; but that uncovered another issue. Let's say the input element is set to select all of it's content (the string) whenever it is focused. Then, each time I click one of my buttons, the content is selected and replaced by the new letter I sent to the WebView.
So, my question is, is there any way to avoid losing the focus of the DOM object when clicking on a XAML button?
NB : I do know about the InputPane, but I can't use it for this app (due to some restrictions on the client side). Also, let me know if something is unclear, and I'll edit asap.
NB2 : I have absolutely no access to the source code of the pages that will be displayed by the WebView, nor can I rely on the presence of a specific element.
The Anniversary Update introduced a new property FrameworkElement.AllowFocusOnInteraction for this scenario. Set it to false on your Button and the Button won't take the focus when it is clicked. The focus will stay on the WebView and the selection shouldn't get cleared (if not there's something else going on as well).
Prior to this there wasn't a good solution. The least bad I've seen was to call WebView.Focus() in the Button.Click handler to return the focus, then wait for the focus events to complete before proceeding in the Click hander.
If you need to target earlier systems than the Anniversary Update then you'll need to set AllowFocusOnInteraction from code after checking that it exists with ApiInformation.IsPropertyPresent . You can't do this directly in Xaml, but you can wrap the check in an attached property which can be called from Xaml. See my blog entry ComboBox on a Flyout attached to an AppBarButton loses mouse input on 1607 for sample code.

How to make that textbox lost focus in WPF?

I have textBox.I begin write something in textbox,and it opens popup with listBox.When i select any item from listBox,textBox losts focus.How to make that listbox not to take focus,or how to get focused textbox?I use MVVM
The simplest way would be just to set focus manually to the textbox whenever it loses the focus. Mind however that this may automatically close your popup, so you'll need to tweak it as well.
By the way, maybe you just need a combobox instead? It's exactly a textbox with a drop-down list of choices.
(MVVM doesn't matter here, the problem is purely in the view.)

Categories