Using converters for DataGrid Column - c#

I create my windows databainding infrastructure with MVVM pattern. I have method which return data for my DataGrid. Problem that I want some columns in DataGrid use Converter (IValueConverter) but I bind my DataGrid to the data directly. I can't strongly type columns specification in xaml because number of columns can be different. Also I can't get UI element (DataGrid) in ViewModel for change.

Your ViewModel can expose the column collection (structure which can specify column header, type of data, necessary information to use converter) bind that to a DP of your view and your view needs to add the columns to the datagrid after parsing that and preparing datatemplate for datagrid column's cell template and cell edittemplate which specifies the binding source, converter.

Related

Setting datagrid columns headers

I have a datagrid (from WPF Toolkit) which has some columns.
Columns headers are set from xaml.
Then from view code-behind, there are places where I do below:
MyDg.Columns[10].Visibility = Visibility.Collapsed;
So the problem with this is that when I remove or add some columns to the datagrid then I need to modify the index set in the above expression depending on if the column I remove or add is before that referenced in the above expression.
So I would like to write the above expression in some manner to avoid the dependency with datagrid columns addition or removal process.
For example defining some constants in view code-behind for headers and from the view (xaml) assing these constants to datagrid as header names. Then from view code behind I could find the index from the column header and proceed:
var index = dataGrid.Columns.Single(c => c.Header.ToString() == "HeaderName").DisplayIndex;
dataGrid.Columns[index].Visibility = Visibility.Collapsed;
I also have thought about binding a property from view code-behind to each datagrid column header but in the view code-behind constructor I set the datacontext to the view model.
So any idea on how to do this?

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.

Bind a GridView to a GridView object in a ViewModel

I'm quiet new in MVVM, and I have a little probleme I can't solve.
I have a GridView and I would like to bind it to a GridView object in my viewmodel (which is separated from the code behind file of my XAML file).
But there's no attribute I can't write with {Binding ...}, and I dont want to give it in the arguments of the initialization of my view model.
I want to have access to all the properties of my GridView in the viewmodel, to get the selecteditem(s).
Thanks
The gridview class represents a view mode that displays data items in columns for a ListView control. About the only useful thing you can bind on the gridview is the Columns property.
If you want to bind rows for the gridview, bind at the listview level to the ItemsSource 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.

Categories

Resources