Xceed Datagrid Cell Colouring

891 Views Asked by At

I have an xceed WPF Datagrid that I want to color a particular cell in each row a particular way.

The grid is bound to a Collection of Bid objects. The column I want to apply to color is BidValue.

    <xcdg:DataGridCollectionViewSource x:Key="BidViewSource" Source="{Binding Bids}" 
                                       d:DesignSource="{d:DesignInstance {x:Type models:Bid}, CreateList=True}">...

       <xcdg:DataGridControl Name="BidGrid" DockPanel.Dock="Bottom" VerticalAlignment="Top"  AutoCreateColumns="False" 
                              ReadOnly="True" ItemsSource="{Binding Source={StaticResource BidViewSource}}"...

In order to simply the process, Bid.BackgroundColor and Bid.ForegroundColor exist for the purpose of supplying getters that determine the correct Color that BidValue should be displayed in.

Basically what I'm trying to do should begin something like this:

                <xcdg:Column FieldName="BidValue" Title="Bid" CellHorizontalContentAlignment="Center" MaxWidth="75" AllowSort="False">
                    <xcdg:Column.CellContentTemplate>
                        <DataTemplate>
                            <DataTemplate.Triggers>

The remaining part that connects it to my the color fields in the Bid object is proving difficult. I've tried to implement the coloring logic in XAML (which is more common) with something like this:

                          <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=BidValue}" Value="X" >
                                    <Setter Property="Background" Value="Red"/>
                                    <Setter Property="Foreground" Value="White"/>
                                </DataTrigger>

but when I do I get the following:

error MC4109: Cannot find the Template Property 'Background' on the type 'System.Windows.Controls.ContentPresenter

1

There are 1 best solutions below

4
JoeHz On

This code actually gets the data from one column (BidText) to use set the Color of another (BidValue) column -- which is its own mean feat using xceed DataGrids.

As alluded to above, a control (a textblock in this case) has to be set in the column's template and bound to the data that was already being displayed. The XAML for referring to another xceed Datagrid column's content to pass into the ColorConverter is shown in the Background and Foreground property assignments. That reference column doesn't need to be visible as is done here, with the Visibility property set to False.

                <xcdg:Column FieldName="BidText" Visible="False" AllowSort="False"/>
                <xcdg:Column FieldName="BidValue" Title="Bid" CellHorizontalContentAlignment="Center" MaxWidth="50" AllowSort="False">
                    <xcdg:Column.CellContentTemplate>
                        <DataTemplate>
                            <TextBlock Name="TextBlock" Width="50"  HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center" 
                                       Text="{Binding}" FontWeight="Bold"
                                       Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:Cell}}, Path=ParentRow.DataContext.BidText, Converter={StaticResource FGColorConverter}}"
                                       Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:Cell}}, Path=ParentRow.DataContext.BidText, Converter={StaticResource BGColorConverter}}"/>
                        </DataTemplate>
                    </xcdg:Column.CellContentTemplate>
                </xcdg:Column>