I have a ListView that gets populated by an ObservableCollection via Binding. Its ListViewItems should have Focusable="False" as I don't want a TextBox to lose keyboard focus when clicking anything. Unfortunately, this prevents ListViewItems from being selected by clicking on them. How can I make ListViewItems selectable by clicking without changing the current focus?
I tried selecting an item in code by doing
myListView.SelectedIndex = i;
but I couldn't find a way to find the index of the clicked item. I can't use VisualTreeHelper.GetChild() to check for IsMouseOver because the data binding does not actually add controls to the visual tree.
Please don't suggest resetting the focus to the TextBox. The TextBox should not lose focus at all.
I figured it out thanks to this question.
I added PreviewMouseDown="OnMouseDown" to my ListViewItem template (in my case to the Border element) which selects the corresponding item by doing:
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
var item = (sender as Border).DataContext;
myListView.SelectedIndex = myListView.Items.IndexOf(item);
}
Related
I have an issue with editable combobox. I can get textbox PART_EditableTextBox and make it multiline by AcceptsReturn="True" and TextWrapping="Wrap".
The problem I have is that I cannot make it work like classic textbox for text operations.
For example key down does not navigate in text and instead just fire item selection even if there is just 1 item in my collection it effectively refresh to original value. Same for scroll bar.
I found that I can catch some events (previewkeydown on parent element/ PreviewMouseWheel) and handle them to not reset value but still missing the text navigation(moving in text by arrows) or having scroll bar.
Edit
I find solution.
Insted of using ComboBox I used ComboBoxEdit from DevExpress.
Now it gets quite easy since they have already implemented property to solve halve of my problems.
this.ComboBoxEdit.AllowSpinOnMouseWheel = false
this.ComboBoxEdit.AcceptsReturn = true;
this.ComboBoxEdit.TextWrapping = TextWrapping.Wrap;
Only problem left was text navigation by arrows.
I catched PreviewKeyDown from parent element and implemented this.
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.OriginalSource is TextBox tx)
{
if (e.Key == Key.Up)
{
EditingCommands.MoveUpByLine.Execute(null, tx);
e.Handled = true;
}
else if (e.Key == Key.Down)
{
EditingCommands.MoveDownByLine.Execute(null, tx);
e.Handled = true;
}
}
}
I think the plan seems like a bad idea.
There are fundamental mismatches between functionality an editable combobox gives and what you seem to require from a multiline textbox inside one.
For one, the arrows already have an inherent meaning to a combobox. That's how you navigate the autocomplete list using keyboard.
An editable combobox is intended to do autocomplete as well as possibly add a new item.
Maybe you could have a textbox and a combobox. The user switches between these using a togglebutton or some such. Depending on whether they want to add new or look up an existing item.
I think your best bet is likely to build your own usercontrol if you particularly need this to look similar to a combobox.
The editable part would be a textbox.
The list of possibles can be a listbox inside a popup or expander.
You could choose whether to use a togglebutton to show your popup like the combobox does, or not.
Add the functionality you need without the problems functionality inherent to a combobox control adds.
Well, I created an editor program (football) where the main focus is a combobox.
Which constantly has to be triggered to select the desired team and make changes and etc.
Only it gets very boring and impractical always having to click with the mouse on it to focus it and select the team with the Down and Up keys,
So what I wanted was to disable the "walk" of the Down and Up keys by the other items,
For example: there are 6 radioButtons and when I select the team in the combobox and then I click on a radioButton to mark the modification and save, I would click on Down or Up and the focus will automatically return to the combobox,
And not go down or up to another radioButton or button understood?
Or if the combobox did not lose its focus ever, no matter how much it clicked on another item.
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MyCombo.Select();
}
There is a combo box on a form.
The Code when the form is loaded.
private void Form_Load(object sender, EventArgs e)
{
combo.Items.Clear();
combo.Items.Add("01");
combo.Items.Add("02");
combo.SelectedIndex = 1;
}
If I scroll the mouse wheel that it will change the index of the combobox.
How to avoid this?
Change selected element on mouse scroll is default behaviour of focused combobox. If you want to suppress this, you can use PreFilterMessage function as described here.
You should change the Tab Order of your controls.Set all tab-order of your controls.
Go form design.Click View in menu bar,then choose Tab Order.
Your combo tab order must not be the first one.
If u want to change from coding,
combo.TabIndex=10;
The problem is your combo is selected at form start.So you scroll mouse wheel,it's selected index changed.
I have a listview with a button as part of the datatemplate. When I click the button I want an action to happen, but I don't necessarily want that item selected. Is there any way I can stop the tap event from bubbling up to the listbox?
Thanks!
Not really sure this is the best solution but. Capture the selected index before you get to the method the button activates. Inside of it at the end set selected back to the original.
I created a sample ListView with a button in it. Clicking on the button does not select the item for me. Can you post your DataTemplate?
But, you can add the following to your click method:
myListBox.SelectedItem = null;
Where myListBox is the name of your ListBox. This will cause the listbox to have no selected items. If you want to keep the previously selected item, you have to keep the track of previously selected item or index and set the listbox to that item using either:
myListBox.SelectedItem = previousItem;
myListBox.SelectedIndex = previousIndex;
Do I programmatically have to manage the backcolor\highlight color on a Listview's item when selected through code?
So if I do this: listView1.Items[1].Selected = true;
Do I also need to do this, so it looks highlight, as it does when selected with a mouse click: listView1.Items[1].BackColor = Color.Blue;
(and clear it when the selection changes)
I would have thought that Selected = true would also do the 'backcolor\highlighting' that happens through the mouse click. Am I missing something?
Has the control got focus? If not the default setting is to hide the selection when the control doesn't have focus - see the HideSelection property.
You do not need to handle the highlighting code yourself, but the item will only appear highlighted if the ListView control has focus. Add listView1.Select() after you select the item and see if that helps.
Otherwise, you'll need to set the HideSelection property on the ListView to false.