Select item programmatically in WPF ListView

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

Getting selected value of a combobox

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)); }

WPF TreeView: How to style selected items with rounded corners like in Explorer

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

Selecting a Textbox Item in a Listbox does not change the selected item of the listbox

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; }

WPF Datagrid set selected row

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

Get selected row item in DataGrid WPF

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

Data binding to SelectedItem in a WPF Treeview

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