Setting datagrid columns headers - c#

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?

Related

How can I change the cell content in a ListView through code?

I'm dynamically creating a WPF window with a grid-like presentation. There is a header row that contains the column headers. It must always be visible. Below that there is a large number of rows (like hundreds) that can be vertically scrolled. Each row contains a text in the first column and a checkbox in each remaining column.
The list of columns and rows is dynamic, so creating a viewmodel class and binding with templates doesn't work (or would at least be very complicated and require much code while giving up the dynamic nature of the problem). Also, with each checkbox interaction in one row, all other rows might be affected (for the same column) and need to be updated. This would need a lot of interaction between all individual row viewmodels.
I'm looking for a solution to create the columns and rows of an existing empty ListView control and populate all of its data, checkboxes and interaction entirely in C# code.
Specifically I'm missing some ListView method that lets me set the content of a specific cell. While I can add columns to the ListView's GridView.Columns collection, I can't go anywhere from there without templates and bindings. Is is possible to use WPF like this?
The problem is that when I use a template for, say, a checkbox column, all checkboxes do exactly the same an no customisation is possible anymore because I don't create each checkbox, instead WPF creates them for me.
You may create a specific CellTemplate for each GridViewColumn using either XamlReader.Parse:
const string Xaml = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x = \"http://schemas.microsoft.com/winfx/2006/xaml\">" +
"<CheckBox Content=\"{Binding Name}\" IsChecked=\"{Binding IsChecked}\" />" +
"</DataTemplate>";
column.CellTemplate = XamlReader.Parse(Xaml) as DataTemplate;
...or a FrameworkElementFactory:
FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
checkBox.SetBinding(CheckBox.ContentProperty, new Binding("Name"));
checkBox.SetBinding(CheckBox.IsCheckedProperty, new Binding("IsChecked"));
column.CellTemplate = new DataTemplate() { VisualTree = checkBox };
This lets you customize the columns but still use the GridView and its functionlity the way you are supposed to. Trying to modifying the generated visual elements is not recommended, especially not if you are using the UI virtualization.

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.

Using a DataSet with a DataGrid

I have a DataSet that I create at runtime. I would like to display this information in a datagrid using wpf and have it display certain columns chosen at runtime.
I can get the data to display using:
datagrid.ItemsSource = dataset.Tables[0].DefaultView;
datagrid.DataContext = dataset.Tables[0];
How do I use DataGridTemplateColumn to add columns to the data grid and have my dataset's information displayed in them or other arbitrary data (numeration, etc)?
Thank you.
Check if these previous StackOverflow questions answer yours:
Generating columns dynamically in the WPF DataGrid?
programatically add column & rows to WPF Datagrid
(Here is the search I used).
Basically you need to add columns to the datagrid, and set their binding (using the Binding property). There are a variety of columns to choose from, you want one that derives from DataGridBoundColumn such as DataGridTextColumn.

Using converters for DataGrid Column

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.

Categories

Resources