32156

DataGrid Selected Cell Background

Question:

I'm having trouble with the WPF DataGrid.

I have the following code..

<Style TargetType="{x:Type DataGridCell}"> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <Border Name="DataGridCellBorder"> <ContentControl Content="{TemplateBinding Content}"> <ContentControl.ContentTemplate> <DataTemplate> <TextBlock Width="auto" Height="auto" Background="Transparent" Text="{Binding Text}" TextTrimming="CharacterEllipsis" TextWrapping="WrapWithOverflow" /> </DataTemplate> </ContentControl.ContentTemplate> </ContentControl> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Orange" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

As you can see, I have replaced the default DataGridCell with a custom template. This was needed as I wanted the cells to have TextTrimming if the text was too big to fit in to the given cell (this is fully shown in the ToolTip when the user hovers over a given cell)

My problem now is that when I select a cell, the foreground get's set to White no matter what I try and do - What I really want to happen is have the cells (or even better, the complete row) background colour change to Orange.

Any help with this would be great

Kris

Answer1:

If you are going to change the DataGridCell.Background property you need to use it somewhere in your ControlTemplate

e.g. using a <a href="http://msdn.microsoft.com/en-us/library/ms742882.aspx" rel="nofollow">TemplateBinding</a>

<Border Name="DataGridCellBorder" Background="{TemplateBinding Background}"/>

Recommend

  • ASP.NET Ajax UpdatePanel flicker
  • WPF - No IsEnabled=true behavior using style with animations
  • ListBox selection state Customization
  • Styling ListView(for custom template) WPF
  • WPF MVVM Binding a ContentConrol's ContentTemplate's StaticResource
  • Asp.net updatepanel Listbox not refreshing layout
  • How to change text in Toggle switch from NuGet?
  • How to insert an image list in the grouped items page befor each group?
  • UpdatePanel does not prevent button from reload page
  • Matching IDs in Update Panel within a Repeater - “already contains a definition for…”
  • ContentControl switch between two buttons
  • How to modify Silverlight template at runtime?
  • Local WPF C# Programming
  • regarding WPF validation;
  • How do I preserve a value entered in a TextBox inside a gridview's templatefield when the gridv
  • How can I bind a string to double in WPF?
  • Using VisualStateManager to start and stop Storyboards
  • Regions already registered error on navigation
  • How to find point on Path from the closest point?
  • WPF Binding View as Content
  • View Injection inside ItemsControl
  • UWP ResourceDictionary is not being loaded when I add a second object to it
  • How to create a WPF-like data trigger in Silverlight?
  • Flyout.showAt() ArgumentException based on Content
  • DropShadowPanel adapt to button template style
  • How to create a fast loading wrapping ListBox?
  • Use a complex type for XAML Binding FallBackValue
  • Apply PivotItemHeader style to PivotItem in UWP
  • doPostBack from C# with JavaScript
  • Having an issue with my TextBox control template
  • Complex Silverlight TreeView, is nested hierarchy possible?
  • Multibinding Multiselection ListView
  • Reassigning an array frees the memory used by it?
  • What's wrong with my datatrigger binding?
  • Caliburn Micro, How to use ContentControl (or display 'sub' ViewModel) using ViewModel Fir
  • NSIS decompiler
  • How to 'create temp table as select' in Slick?
  • How load cv2.KeyPoint and Descriptors correctly on OpenCV 3 with Python 3?
  • How to add a focus style to an editable ComboBox in WPF
  • Paperclip, set path outside of rails root folder