Static column header in WPF datagrid - c#

I want to make a static column header in WPF datagrid, so the user will see the column headers/names, when scrolling down if table is too long.
However, I am really new to this WPF/C# thing, and I am no sure how to do it, and if it will be in cs part, or xaml part. I have searched many possible solutions, but most of them were for asp.net link or I found even nice HTML/CSS solution to this, or even some plugins to do so.
However, my question remains - how to make datagrid column header static, and should I do it in cs part or xaml part?
Thanks
Edit: as I am just fixing larger part of code, it would be too difficult to post it all. However, I read those problems with scrolviewer, and I found there this part of code, which might have caused it:
var scrollViewer = new ScrollViewer()
{
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto
};
scrollViewer.AddHandler(UIElement.MouseWheelEvent, new RoutedEventHandler(this.MouseWheelHandler), true);
var stackPanel = new StackPanel();
scrollViewer.Content = stackPanel;
So - could this caused it, and do you have advice how to fix it? Thanks in advance

Related

How can I create a DataGrid with dynamic columns and cell templates?

I liked this approach to the solution, except that it will not work with virtualization.
Here's the relevant code from that post, abbreviated:
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
DataRowView dataRow = (dataItem as DataRowView);
object cellData = dataRow[cell.Column.DisplayIndex];
var contentHost = new ContentControl() { Content = cellData };
contentHost.ContentTemplate = (DataTemplate)SomeResourceDictionary["SomeResourceKey"];
return contentHost;
}
This method is called by the DataGrid for every cell created/rendered. If virtualization is enabled, then the FrameworkElement created here may be recycled, i.e. used again.
The problem: using the information stored in cell and dataItem the content is determined and a direct reference is passed to the created ContentControl. If this FrameworkElement is reused, it will reference the same content - regardlesss of which row it is being rendered for.
Result: during (quick) scrolling the content displayed in the grid rows will randomly be mixed up.
My question: using cell.Column.DisplayIndex can the ContentControl.Content binding be set without including a direct reference to the desired content? Maybe, as if I defined it in Xaml something like this: "{Binding Path=dataRow[0]}" - but that also doesn't seem quite right.
Any suggestions greatly appreciated!
#BionicCode:
There are two challenges addressed here:
Binding a DataGrid to a source with dynamic columns.
Allow a DataTemplate to be defined for each dynamically generated column.
The first issue is covered quite well in the post linked above: create a DataTable from the original source and bind the DataGrid to that. The difficult part of the second issue - and why DataGridTemplateColumns cannot simply be used - is that DataGridTemplateColumns receive the entire row item as data context, i.e. to access the correct data, the template would have to know which column it is being used in.
This is where the code solution shown above comes in, by manually generating the FrameworkElement and "statically" assigning the desired content.
Luckily, the fix is rather simple, see my answer below.
I knew the solution had to be close and, luckily, it was. In the code above replace the following line...
var contentHost = new ContentControl() { Content = cellData };
...with these:
var contentHost = new ContentControl();
BindingOperations.SetBinding(contentHost, ContentControl.ContentProperty, new Binding("[" + cell.Column.DisplayIndex + "]"));
Now the ContentControl can dynamically bind to the correct column and virtualization works. At least EnableRowVirtualization, I haven't tested EnableColumnVirtualization, yet.

List of CheckBox in DataGridViewComboBoxColumn [Windows Form]

I am trying to show a dropdown composed of several CheckBoxes (a list of CheckBox) in a DataGridView. Specifically, the DataGridView provide a DataGridViewComboBoxColumn that allows to have a dropdown with several items. However, if you try to add as a datasource of the column a list of checkboxes you will find out that the solution is not working (the checkboxes are not shown).
In the following link there is a solution on "regular" ComboBox: https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown
However, I need it for DataGridViewComboBoxColumn. Someone has an idea of what can be done to reach the goal? Thanks for answering (I link an example of the code below)
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
// Put List of Checkboxes in DataGridViewComboBoxCell for each row of the Grid
((DataGridViewComboBoxCell) dataGridView.Rows[i].Cells[1]).DataSource = new List<CheckBox> { new CheckBox(), new CheckBox(), new CheckBox(), new CheckBox() };
}
I have modified DataGridViewColumns in past (OK, not entirely myself, according to some CodeProject cook book too :-) ), so I have some idea.
However this can't probably work in this case, there's a problem with this idea. You'd not only need to change how the cell looks, but also think how to store the data. How would you assign a separate datasource to each single DataGridViewComboboxCell? If possible, that would be probably quite extensive work, probably a class handling the structure and the events.
If I can offer a solution, you can have another object (i.e. another DataGridViw at the side with CheckBoxColumn and TextColumn which would load on DataGridView1.SelectionChanged), or you can populate it in a modal form on CellClick of that cell and feed back a list of selected items strings on close, etc. I did that in past many times and I don't think there's an easy way around it.

Bunifu Dataviz adding Datalabels

I've researched and can't seem to find if adding Data Labels to Dataviz charts is possible. I haven't seen an example that actually shows them. Is this something that is even possible ?
An example of adding a Y-axis label:
var datapoint = new Bunifu.DataViz.DataPoint(Bunifu.DataViz.BunifuDataViz._type.Bunifu_line);
datapoint.addLabely("SUN", r.Next(0, 100).ToString());

OpenXML spacing troubles

I am creating a word document(2010) using c#.
It consists of a large table weith cells and within the cells paragraphs.
I Have achieved this with a little with (i hope simple) a little niggly issue. In front of every piece of text there is a block of empty space.
I am attempting to get rid of this space with no luck.
To build the table I have pretty much followed this example.
http://msdn.microsoft.com/en-us/library/office/gg490656.aspx
I have tried fiddling with the sacing of the cells, of the table rows and of the table cells but i'm unsure if i am using the right properties.
I have tried appending new ContextualSpacing() { Val = false } to table row and cells with no luck. I've also tried new Spacing with no luck either!
Any ideas hints tips would be muchly appreciated
I found that setting the style to a style within word resolved the issue. This was a manual process and then i looked in to how to do this programatically.
DocumentFormat.OpenXml.Wordprocessing.TableStyle tableStyle2 = new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "TableGrid" };
Append this item to the table and it all worked perfectly!

How to show the Small Pointer in WPF DataGrid as in WinForms DataGridView

I am working on WPF and DataGrids, I am really new on this. And I need the WPF DataGrid to show in its row headers the small pointer as in the WindowsForms DataGridView does (it looks like a small triangle pointing to right).
I have tried with this property:
dataGrid1.HeadersVisibility = DataGridHeadersVisibility.All;
But doing as above the dataGrid got some very small headers which seems to be just to select the entire row.
I have done some research about it but I have found nothing.
Does anyone know if this is even possible?
Thank you in advance.
2 things --
1) specify a SortMemberPath -- this will get you the default style 2) style the grid's header template
After some research I have found two links which allow me to do what I was trying to accomplish:
WPF DataGrid Row indicators
or this one:
Changing the style of the selected row of the DataGrid control

Categories