Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid edit text wrong color theme - Version 2023 #28

Open
ricaun opened this issue Aug 2, 2023 · 9 comments
Open

DataGrid edit text wrong color theme - Version 2023 #28

ricaun opened this issue Aug 2, 2023 · 9 comments

Comments

@ricaun
Copy link

ricaun commented Aug 2, 2023

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
Copy link
Author

ricaun commented Aug 2, 2023

Something strange happens as well with ComboBox inside the DataGrid.

image

@AngryCarrot789
Copy link
Owner

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

@AngryCarrot789
Copy link
Owner

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
Copy link
Owner

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

@ricaun
Copy link
Author

ricaun commented Aug 3, 2023

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
Copy link
Author

ricaun commented Aug 3, 2023

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. 😅

@HiMarioLopez
Copy link

HiMarioLopez commented Aug 12, 2023

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 😅)

@AngryCarrot789
Copy link
Owner

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

@shivan
Copy link

shivan commented Jan 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants