WPF ListView SelectedItem is null
It’s very easy, just handle Click event on your checkbox: private void CheckBox_Click(object sender, RoutedEventArgs e) { var cb = sender as CheckBox; var item = cb.DataContext; myListView.SelectedItem = item; }
It’s very easy, just handle Click event on your checkbox: private void CheckBox_Click(object sender, RoutedEventArgs e) { var cb = sender as CheckBox; var item = cb.DataContext; myListView.SelectedItem = item; }
Bind the IsSelected property of the ListViewItem to a property on your model. Then, you need only work with your model rather than worrying about the intricacies of the UI, which includes potential hazards around container virtualization. For example: <ListView> <ListView.ItemContainerStyle> <Style TargetType=”ListViewItem”> <Setter Property=”IsSelected” Value=”{Binding IsGroovy}”/> </Style> </ListView.ItemContainerStyle> </ListView> Now, just work with your … Read more
ListViews by default don’t have a choiceMode set (it’s set to none), so the current selection is not indicated visually. To change this, you just need to set the choiceMode attribute of your ListView to singleChoice. If you’d like custom background for the selected items in your list, you should also set the listSelector attribute. … Read more
final ListView lv = (ListView) findViewById(R.id.ListView01); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) { String selectedFromList =(String) (lv.getItemAtPosition(myItemInt)); } }); I hope this fixes your problem.
Try this: private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { ComboBox cmb = (ComboBox)sender; int selectedIndex = cmb.SelectedIndex; int selectedValue = (int)cmb.SelectedValue; ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem; MessageBox.Show(String.Format(“Index: [{0}] CarName={1}; Value={2}”, selectedIndex, selectedCar.Text, selecteVal)); }
Adding to @Sheridan’s answer This isn’t a 100% accurate but should get you pretty close (it’s using the colors from GridView which is pretty close to Windows Explorer) <TreeView …> <TreeView.Resources> <LinearGradientBrush x:Key=”{x:Static SystemColors.HighlightBrushKey}” EndPoint=”0,1″ StartPoint=”0,0″> <GradientStop Color=”#FFD9F4FF” Offset=”0″/> <GradientStop Color=”#FF9BDDFB” Offset=”1″/> </LinearGradientBrush> <LinearGradientBrush x:Key=”{x:Static SystemColors.ControlBrushKey}” EndPoint=”0,1″ StartPoint=”0,0″> <GradientStop Color=”#FFEEEDED” Offset=”0″/> <GradientStop Color=”#FFDDDDDD” Offset=”1″/> </LinearGradientBrush> … Read more
We use the following style to set a PreviewGotKeyboardFocus which handles all events of TextBox control and ComboBoxes and such: <ListView.ItemContainerStyle> <Style TargetType=”ListViewItem”> <EventSetter Event=”PreviewGotKeyboardFocus” Handler=”SelectCurrentItem”/> </Style> </ListView.ItemContainerStyle> And then we select the row in code behind: protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e) { ListViewItem item = (ListViewItem) sender; item.IsSelected = true; }
My code iterates through cells of the datagrid‘s first column and checks if cell content equals to the textbox.text value and selects the row. for (int i = 0; i < dataGrid.Items.Count; i++) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i); TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock; if (cellContent != null && cellContent.Text.Equals(textBox1.Text)) { object item = dataGrid.Items[i]; … Read more
You can use the SelectedItem property to get the currently selected object, which you can then cast into the correct type. For instance, if your DataGrid is bound to a collection of Customer objects you could do this: Customer customer = (Customer)myDataGrid.SelectedItem; Alternatively you can bind SelectedItem to your source class or ViewModel. <Grid DataContext=”MyViewModel”> … Read more
I realise this has already had an answer accepted, but I put this together to solve the problem. It uses a similar idea to Delta’s solution, but without the need to subclass the TreeView: public class BindableSelectedItemBehavior : Behavior<TreeView> { #region SelectedItem Property public object SelectedItem { get { return (object)GetValue(SelectedItemProperty); } set { SetValue(SelectedItemProperty, … Read more