Set focus in Listbox in MVVM pattern - c#

I am trying to set a focus on the first Item of listBox. I am binding the value to listbox, contaning a textbox by a onpropertychange method and I want to set focus in the first item. how can I achieve that?

you can set focus on the first item with the following:
listBox1.Items[0].Focused = true;
you may also want to have the item that is focused to be selected, if you do then:
listBox1.Items[0].Selected = true;
Just remember the [0] above means the first index. [1] would be the second and so on.

Related

Eliminate null entry in combobox

Is there any property to remove the first (and empty) item in a combobox with style DropDownList ? In other words, I would like to choose the default selected item for a combobox.
I know i can validate the selected item with code, but i want to avoid showing message boxes to the user.
Set the comboBox.SelectedIndex property to 0 to set the selection to the first item in the combobox.
You should set the Text or SelectedIndex or SelectedValue property. In this way the combobox updates the text that's is showing and removes the first empty item (that actually is not a real item).

Focus is not set in UserControl WPF

I cannot able to set focus on User Control Item
I am using DataGrid in User Control
I want to set focus on First row of DataGrid
I found following code
int index = 0;
dgAccountInfo.SelectedItem = winAccountInfoGrid.dgAccountInfo.Items[index];
dgAccountInfo.ScrollIntoView(winAccountInfoGrid.dgAccountInfo.Items[index]);
DataGridRow dgrow = (DataGridRow)dgAccountInfo.ItemContainerGenerator.ContainerFromIndex(index);
if (dgrow != null)
{
dgAccountInfo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next))
};
By running above code I found ContainerFromIndex method always returns null
To rid of null problem I found following link
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ab95dd62-995f-481a-a765-d5efff1d559c/
I come out of null problem using above link but still focus is not set on data grid
Thanks for reading my question
It appears that, in the code you're using, you are data binding to something else, which is unnecessary if you are only wanting to set the focus to the first item. If your only intention is to set the focus (selected index) of the datagrid, that can be accomplished in one single block of code.
if (dgAccountInfo.HasItems){
dgAccountInfo.SelectedIndex = 0;
}
The if construct ensures that your datagrid actually has items in it (otherwise, the next line of code would throw an exception). If the datagrid has items, it executes the code inside the construct. If the datagrid has no items, it skips right over the code.
Even if you never plan to have no items in your datagrid, you should include this if construct anyway, in the off chance that something does happen
Inside the if construct, you set the datagrid's SelectedIndex to the first item. Remember that, like with a list box, a datagrid is "zero-based," meaning that the first item has an index of zero. To select nothing in the datagrid, set the selected index to "-1"
In the end, setting "SelectedIndex" is far more reliable than setting "SelectedItem," as the items in a datagrid usually change.
If you wanted, you could include an #else statement in the above code (just above endif) if you wanted to do something else if the datagrid has no items.
I hope this helps!

ListView problem: it does not Jump to the selected item

Ok I am saying lsvAvailable.Items[index].Selected = true;
and HideSelection is false so it shows the gray back color on the selected item, which is what I want .. but if the found item is somewhere down the list so I need scrolling to SEE it. it does Not JUMP to that item...and still I need to ba able to continue typing ( let's say I am typing in a text box like a search box and it is showing the item in the listView)
Call the ListViewItem.EnsureVisible method:
lsvAvailable.Items[index].EnsureVisible();

selection issue in listview or TreeView

Hello i have a list view control,
While the form is being loaded i fill the list, i have aprrox. say 100+ items.
While filling i check some parameters and decide which item/row need to be selected.
i set the Selected property to true... refer the code below:
some lines here .....
ListViewItem listViewItem = new ListViewItem("COL1");
listViewItem.SubItems.Add("COL2");
check for some condition and then
listViewItem.Selected = true;
this.m_lstViewCtrl.Items.Add(listViewItem);
This does select the item, there are no issues with it...
however, say the ctrl is sized to see onlu say some 15 items, but the selection is say some 35th item.... currently the scroll bar appears the item is selected but i have to scroll to see what was selected?
is it possible to scroll to the selected item so that is selection is clearly visible...
Will the same apply for a Treeview?
Use the EnsureVisible property on the ListViewItem.
UPDATE: So, your code would be as follows:
listViewItem.Selected = true;
listViewItem.EnsureVisible();
this.m_lstViewCtrl.Items.Add(listViewItem);

How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?

Using C#, say you have a ComboBox that has a DropDownStyle set to DropDown (there are items in the drop down but the user can manually enter a value as well). How do you set a default value for the ComboBox that is not in the list of values in the drop down, but begins with text from a possible selection? Normally setting ComboBox.Text works fine, but if there is an item in the drop down that begins with the text you want as the default, it automatically selects the first item in the list that starts with the text. For example:
Values in the drop down:
c:\program files\
c:\windows\
d:\media\
Default Value Assignment
myComboBox.Text = "C:\";
Result
Initial value of ComboBox when the form opens is "c:\program files\".
So what am I doing wrong? How do I correctly set a default value of an item not in the drop down list that begins with a possible selection?
Does the following code work?
myCombo.SelectedIndex = myCombo.FindString(#"c:\");
Note: I haven't tried it. Looked up for properties/methods that could help using reflector.
I couldn't repro the behavior you are describing. Adding the three values via the Items collection and then setting the initial value to "c:\" (you omitted an # in your code sample, by the way) worked fine. My guess is that something else in your code is setting the value of the combo box after you set it.
I was able to get this to work with having the items in the ComboBox be ComboBoxItems (I dont see why this wouldn't work with other types). Set the ComboBox.Text like you are and make sure SelectedIndex = -1 and also you need IsEditable = True.

Categories

Resources