WPFDarkTheme icon indicating copy to clipboard operation
WPFDarkTheme copied to clipboard

DataGrid edit text wrong color theme - Version 2023

Open ricaun opened this issue 2 years ago • 9 comments
trafficstars

Hello @AngryCarrot789

First of all great update! I was trying to update the DataGrid but I having a hard time, wpf style is not my best skill.

I found that the DataGrid when editing the text is not changing to the proper theme color.

image

I don't know how to fixed that yet. 😑

ricaun avatar Aug 02 '23 18:08 ricaun

Something strange happens as well with ComboBox inside the DataGrid.

image

ricaun avatar Aug 02 '23 19:08 ricaun

I'll try and fix this. The microsoft docs about data grid styles are pretty lacking

AngryCarrot789 avatar Aug 02 '23 21:08 AngryCarrot789

I looked at the source code and it seems like it's not possible to set the implicit styles... The DataGridCell contains a ContentPresenter whose content ends up being set directly to the control such as ComboBox, TextBox, etc. Those controls are created in the types that extend DataGridColumn (such as DataGridComboBoxColumn) which do contain the ElementStyle and EditingElementStyle properties, but because DataGridColumn extends DependencyProperty, you can't create an implicit style to set those properties because styles can only be applied to things that derive from FrameworkElement. I hope to find another way but this looks like it's a limit with WPF... so you might have to just manually generate your data grid columns and set those styles (I think something like EditingElementStyle={StaticResource {x:Type ComboBox}} will work)

AngryCarrot789 avatar Aug 02 '23 23:08 AngryCarrot789

I added a new update with the DataGridTextColumnElementStyle, DataGridTextColumnEditingElementStyle, DataGridComboBoxColumnEditingElementStyle, DataGridCheckBoxColumnElementStyle and DataGridCheckBoxColumnEditingElementStyle styles which you can apply to the respective columns. I'm not sure why the combo box's drop down's selected items have no background though

AngryCarrot789 avatar Aug 03 '23 00:08 AngryCarrot789

I'm not sure why the combo box's drop down's selected items have no background though I was testing the new code and something change in this new version that breaks the ComboBox selected color and mouse over color inside de DataGrid.

ricaun avatar Aug 03 '23 12:08 ricaun

I was messing around with the AutoGeneratingColumn event if I change EditingElementStyle and ElementStyle to kinda work.

image

In the old version works as well and the ComboBox in the DataGrid works perfect.

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var dataGrid = sender as DataGrid;
    if (e.Column is DataGridTextColumn dataGridTextColumn)
    {
        if (new DataGridTextColumn().ElementStyle == dataGridTextColumn.ElementStyle)
        {
            dataGridTextColumn.ElementStyle = null;
        }
        if (new DataGridTextColumn().EditingElementStyle == dataGridTextColumn.EditingElementStyle)
        {
            dataGridTextColumn.EditingElementStyle = null;
        }
    }

    if (e.Column is DataGridComboBoxColumn dataGridComboBoxColumn)
    {
        if (new DataGridComboBoxColumn().EditingElementStyle == dataGridComboBoxColumn.EditingElementStyle)
        {
            dataGridComboBoxColumn.EditingElementStyle = null;
        }
    }

    if (e.Column is DataGridCheckBoxColumn dataGridCheckBoxColumn)
    {
        if (new DataGridCheckBoxColumn().ElementStyle == dataGridCheckBoxColumn.ElementStyle)
        {
            dataGridCheckBoxColumn.ElementStyle = null;
        }
        if (new DataGridCheckBoxColumn().EditingElementStyle == dataGridCheckBoxColumn.EditingElementStyle)
        {
            dataGridCheckBoxColumn.EditingElementStyle = null;
        }
    }
}

This is the code I'm using to compare if the datagrid column has a default style if is the case set to null. 😅

ricaun avatar Aug 03 '23 13:08 ricaun

image

image

Looks like we're seeing the same issue, unrelated to the DataGrid, but related to the ContentPresenter that's a part of the Extended WPF Toolkit's CheckComboBox. So a widespread issue with implicit styles applied to ComboBox-like elements overwriting (?) pre-defined styles.

Not sure if this adds anything at all to the discussion. I'm definitely lost when it comes to putting together a potential solution (even a work-around 😅)

HiMarioLopez avatar Aug 12 '23 04:08 HiMarioLopez

It could be because you don't have a style for the CheckComboBox?

If you do though... well I haven't used the toolkit myself, but at a guess based on what WPF does sometimes, maybe there's some resource keys that they have defined that allows you to override the default styles? WPF does this with the menu item separator, data toolbar, etc:

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" />

So if you can find them, you might be able to just use the above xaml to 'delegate' the styles

AngryCarrot789 avatar Aug 12 '23 07:08 AngryCarrot789

Same problem here. I fixed it this way:

private void grdAccurateWorkTime_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
	if (isAnyRowEditing())
	{
		e.Cancel = true;
	}
        //...
        if (e.Column is DataGridComboBoxColumn)
	{
		DataGridComboBoxColumn cbCol = (DataGridComboBoxColumn)e.Column;

		Style comboboxStyle = new Style(typeof(ComboBox), (Style)TryFindResource("DataGridComboBoxColumnEditingElementStyle"));
		if (comboboxStyle != null)
		{
			cbCol.EditingElementStyle = comboboxStyle;
		}
	}
}

But would be easier, if it works out of the box.

shivan avatar Jan 11 '24 08:01 shivan