locked
Set DataGridTextColumn background when in edit mode?? RRS feed

  • Question

  • Hi,

    I need to be able to set the background colour for all DataGridTextColumns when it is in edit mode. I prefer to avoid writing xaml on each instance of this type of column to get this to work if possible. Is this possible?

    Ive tried the following below, which correctly sets the background when the cell is not in edit mode, but not when editing.

    Regards

    Snippet

    <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="Background" Value="Green" />
                    <Setter Property="BorderBrush" Value="Green" />
                </Trigger>
                <Trigger Property="DataGridCell.IsEditing" Value="True">
                    <Setter Property="Background" Value="Yellow" />
                    <Setter Property="BorderBrush" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>

    Wednesday, March 23, 2011 3:13 PM

Answers

  • You are right. My sample will set only the border. I feel you may fix this issue using attached property and set the EditingTemplateStyle. The below thread will help you in that.

    http://stackoverflow.com/questions/2630292/why-i-cant-style-the-datagridtextcolumn


    Ajosh Jose
    • Marked as answer by Devman81 Friday, March 25, 2011 9:22 AM
    Thursday, March 24, 2011 2:24 PM
  • Hi Devman81,

    You are correct. The cell's background is correctly changed to Yellow but the TextBox has a White background on top of it.

    How about override the default system color key to change all the textbox's background? By default, the TextBox use SystemColors.WindowBrushKey as the background, so we can override this key in the resource system to change the TextBoxes' background. It may change the colors on other control from white to transperent if they also use this system color key. But it is the easiest way to accomplish what you want.

    <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Transparent"/>
    

    Alternatively, we can manually change the styles for each column, or we can also use Jose's suggestion about attached property to do this.

    Hope this helps.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Devman81 Friday, March 25, 2011 9:21 AM
    Friday, March 25, 2011 2:58 AM

All replies

  • Try this

     <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                  <Trigger Property="DataGridCell.IsEditing" Value="True">
                    <Setter Property="Background" Value="Yellow" />
                    <Setter Property="BorderBrush" Value="Red" />
                  </Trigger>
                  <MultiTrigger>
                    <MultiTrigger.Conditions>
                      <Condition Property="DataGridCell.IsSelected" Value="True" />
                      <Condition Property="DataGridCell.IsEditing" Value="False" />
                    </MultiTrigger.Conditions>
                      <Setter Property="Background" Value="Green" />
                      <Setter Property="BorderBrush" Value="Green" />
                   
                  </MultiTrigger>
                </Style.Triggers>
              </Style>


    Ajosh Jose
    • Edited by Jose Atlanta Wednesday, March 23, 2011 4:44 PM Format issue
    Wednesday, March 23, 2011 4:43 PM
  • Hi Jose Atlanta,

    The Xaml you provided doesn't solve the issue either. The Background remains white and is not set to Yellow in this case.

    Im guessing the cause of the issue is the Cell Editing template contains a textbox style element and its this elements background that needs to be made transparent. However, I do not know how to do this without setting the Cell Edting Stye for each instance of DataGridTextBoxColumn.

    Thursday, March 24, 2011 9:22 AM
  • You are right. My sample will set only the border. I feel you may fix this issue using attached property and set the EditingTemplateStyle. The below thread will help you in that.

    http://stackoverflow.com/questions/2630292/why-i-cant-style-the-datagridtextcolumn


    Ajosh Jose
    • Marked as answer by Devman81 Friday, March 25, 2011 9:22 AM
    Thursday, March 24, 2011 2:24 PM
  • Hi Devman81,

    You are correct. The cell's background is correctly changed to Yellow but the TextBox has a White background on top of it.

    How about override the default system color key to change all the textbox's background? By default, the TextBox use SystemColors.WindowBrushKey as the background, so we can override this key in the resource system to change the TextBoxes' background. It may change the colors on other control from white to transperent if they also use this system color key. But it is the easiest way to accomplish what you want.

    <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Transparent"/>
    

    Alternatively, we can manually change the styles for each column, or we can also use Jose's suggestion about attached property to do this.

    Hope this helps.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Devman81 Friday, March 25, 2011 9:21 AM
    Friday, March 25, 2011 2:58 AM
  • Jose Atlanta / Min Zhu

    Thanks to both of you for your answers. I haven't decided which one I'll use yet.

    Best Regards

    David

    Friday, March 25, 2011 9:30 AM