C# WPF datagrid: ItemsSource - c#

I want to know how you can modify properties of columns of a WPF toolkit datagrid once the ItemsSource has been set (it is set in XAML)? For example I want to make a specific column have the property IsReadOnly equal to true.
So basically there are two things I want to know:
-How can I get access to a specific column once the ItemsSource has been
set?
-How can I change the properties of a specific column once the ItemsSource
has been set?

Answer to both questions is:
By looping through your datagrid
Access column by it's index

Related

WPF Datagrid restrict input with combobox

i am using the EF and the MVVM approach to get a bunch of data records in an ObservableCollection. This ObservableCollection lives in a ViewModel.
The ViewModel is than bound to a DataGrid. This works just fine.
For the sake of data consistency i now want to turn a bunch of columns in the DataGrid into ComboBoxes. So the user can only choose from a predefined set of values. These predefined values are also stored in the database (You can think of it as a lookup table). I can load the values from the database and also put it in an ObservableCollection. But now i wonder how can i bind this to the DataGrid, since i can only specify one ItemSource for the DataGrid.
Is this even the right approach in WPF?
Bind the ItemsSource property of DataGridComboBoxColumn to your values from the lookup table.
Then bind SelectedValueBinding to the field in the main collection.
If you are using normalised values (i.e. numeric values mapped to the items in the lookup table), then you should also set the SelectedValuePath and DisplayMemberPath properties.

WPF: add dynamic ToolTip to DataGridCell

I'm developing my first WPF application that queries a database and shows some records of some tables in TabControl if one or more fields of these records not satisfy certain condition.
I have a DataTable as data source and I use a DataGrid to show results (i.e. the wrong records). I'd like to use ToolTip on DataGridCell to indicate why a field is considered wrong. There's a way to iterate over the DataGridRow and the DataGridCell so that I can set dynamic ToolTipfor every specific field?
Thanks in advance.
I would bind the DataGrid selected item to a SelectedRecord property in my view model (where the data source is coming from), see Get selected row item in DataGrid WPF as an example. The SelectedRecord property would then set the SelectedRecordToolTip property in accordance to the SelectedRecord value (i.e. using a dictionary with the error as the key and the tooltip as the value). Finally, you can then bind your tooltip to the SelectedRecordToolTip property.

Filter based on value of adjacent cell of GridView/ListView in WPF

I have a ListView with a variable number of items in a WPF project with two columns. Column A is a string, column B is a combobox. I have the ListView bound to some collectionView, and the combobox column is a cellTemplate, binding the combobox to some other collectionView or observableCollection.
Is there some way to filter the collection shown in the combobox in column B dynamically based on the value of column A?
I am open to replacing the ListView with some other control if it would grant me the functionality described.
Perhaps you could achieve this by binding the combobox property B's ItemsSource to
{Binding Path=PropertyA, Converter={StaticResource ItemsConverter}}
Then write a IValueConverter which takes a TypeA and returns a IEnumerable. Whack that into the Resources so the StaticResource can find it.
Bob's your uncle.

Finding items in listview by typing first letter WPF

I have a listview which binds customer informations from database. There are 15 columns which are binded in that listview. One of that column is Customer Name.
I want to be focused them when I type their name's initial character from the keyboard. Have you any idea to make this ?
This is my listview's XAML code
<ListView x:Name="datalist" ButtonBase.Click="datalist_Click" ContextMenuOpening="datalist_ContextMenuOpening" MouseDoubleClick="datalist_MouseDoubleClick" SelectionChanged="datalist_SelectionChanged"
MouseUp="datalist_MouseUp" PreviewMouseUp="datalist_PreviewMouseUp" >
Try to operate only on Model/ModelView and not on UI directly (as much as it possible).
For example, define in ModelView a property
public bool Focused{
get ..
set... //OnPropertyChanged
}
nd bind it to the UI elements corresponding property.
After this the only thing you need to do is t simply
find an element in binded data (ModelView objects)
set it's Focused property to true

How to persist column index of a WPF datagrid?

I want to persist the index value of each column of my own datagrid but I don't know how to do it. Looks like the index can only be set thru the order of addition of the columns on the xaml code.
Any help will be great, thanks.
I found the solution, columns have a DisplayIndex property that I can bind to my column ViewModel.

Categories

Resources