WPF DataGrid get selected row - wpf

Need to do some logic based on the current row selected. New to WPF before I would do something like this;
int i myDataGridView.CurrentCell.RowIndex
Now that is not available, what is best way to get selected row?

Use DataGrid.SelectedItem property. It gives the Selected Item on the DataGrid if SelectionMode is Single. Else DataGrid.SelectedItems gives the multiple selection if SelectionMode is Extended or Multiple

Try this:
SomeObject someObject = (SomeObject)myDataGridView.SelectedItem

Related

WPF window inserting an array into DataGrid

WPF window in C# doesn't have option of Inserting something into DataGrid immediately, like as WinForms Form
DataGridView.Rows.Add(whatever)
What is alternative for this code 👆?
So how can I insert an array into DataGridView in WPF window?
You can actually add objects directly to the Items property of the DataGrid:
dataGrid.Items.Add(whatever);
But if you want to be able to edit the items, you should set or bind the ItemsSource property to an IList:
dataGrid.ItemsSource = new List<object> { whatever };
You need to bind the DataGrid to an ObservableCollection. If you insert into the ObservableCollection, it'll appear on the UI

remove 1 colum from a datagrid using item source

I have a datagrid which has an item source of observablecollection and I want to 1 column to be saved int the observablecollection and not show it to the user in the data grid.
how can I do that?
You can hide the Column in the code behind like this:
YourDataGrid.Columns[IndexOftheColumn].Visibility = Visibility.Collapsed;
Alternatively if you want this to be xaml-only, you should set AutoGenerateColumns to False and define custom columns in your Xaml.

Set VisualState of combobox when a item is selected in Xaml (Silverlight)

When my combobox expands and I select an item, I want the combobox to change visual state(it is highlighted). This will signify something is selected. I tried various VisualStates but none of them would trigger in this scenario. How can I achieve this? Thanks.
The standard ComboBox simply doesn't have states to distinguish between having something selected and having nothing selected.
There are a number of ways to go about solving the underlying problem, and it depends mostly on the answer to the following question:
Do you really need to change the visual appearance of the ComboBox itself or does it suffice to style the selected item more prominently?
If it's the latter, you're best served with the rather easy way of using a custom control template for the ComboBoxItems.
If you really want to style the ComboBox itself that way, there are two options I can think of:
A) Add custom states to a ComboBox with a custom template.
Copy your ComboBox's control template and add another state group to the already present states. Both of this is typically done in Expression Blend.
After that you can update the new states in code with
VisualStateManager.GoToState(this, "Selected", true);
for example. You will have to set those states yourself when the first item is chosen. This could be done on the SelectionChanged event.
B) Derive from ComboBox
If you want to use the control in this way often, it might be worthwhile to derive from ComboBox to make your own custom control.
It would look somthing like this:
[TemplateVisualState(Name = "SelectedStates", GroupName = "Unselected")]
[TemplateVisualState(Name = "SelectedStates", GroupName = "Selected")]
// ... (more attributes copied from the ComboBox ones)
public class MyComboBox : ComboBox
{
public MyComboBox()
{
SelectionChanged += HandleSelectionChanged;
DefaultStyleKey = typeof(MyComboBox);
}
void HandleSelectionChanged(object sender, SelectionChangedEventArgs e)
{
VisualStateManager.GoToState(this, SelectedItem != null ? "Selected" : "Unselected", true);
}
}
And you would then need a default style based on the default ComboBox style (or whatever you usually use).
Note that I didn't test this in any way.

Selecteditem event in MVVM silverlight

i have a datagrid bound to a property. In this grid i have columns which consists of cells which are like hyperlink i mean when user clicks on the cell value based on these values another gird will get populated. i want to know how to get the cell value and pass it to some method so that other grid will get populated.
The best way to do this is in your viewmodel.
You should bind the SelectedItem of your datagrid to a new property in your ViewModel. In the set method of this new Property, call a new method to populate a new ObservableCollection/List/whatever...
Finally, bind your "other grid" ItemsSource to this new observable collection from your ViewModel.
Edit:
If you need to load one thing or another depending on the column you are going to use the code behind, take a look at this:
Silverlight DataGrid how to get cell value from a selected item?

How to bind a custom value to the Text property of a TextBlock inside of a ListBox DataTemplate?

I would like my ListBox to number each ListItem using its index + 1.
How would I do that to the Text property of a TextBlock in a DataTemplate of the ListBox?
If each ListBoxItem uses SelectedIndex + 1, they will all display the same value since SelectedIndex is a scalar. Moreover, this number will change as the user selects different ListBoxItems. I suspect you actually want to display each item's index within the ListBox + 1.
To achieve this, you're probably best off using the ListBox's ItemsContainerGenerator to get the index of the item within the container (see the IndexFromContainer method). You could look at exposing this from your data class, or perhaps look into an attached readonly property that retrieves this value for you.
I had the same question. So far I'm just using my data model to provide the numbers...

Resources