-1

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

JoeHz
  • 2,136
  • 1
  • 18
  • 29
  • i haven't used xceed toolkit but check your style using blend of your data grid, it is because content presenter hasn't any property background – Shubham Sahu Jan 05 '19 at 09:10
  • Yeah, I'm trying to get to where the background property exists – JoeHz Jan 05 '19 at 09:25
  • Your data template needs to be defined to some specific control, like a `TextBlock`, for instance. Also, you probably want to compare `BidValue` to a range, like `BidValue > threshold`, and not to any particular value. In that case, you're going to need a `Converter`, not a `DataTrigger`. – jsanalytics Jan 05 '19 at 12:39
  • What control would I be referring to when I want a particular cell on the datagrid? – JoeHz Jan 05 '19 at 23:59
  • If you need to refer directly to a cell in a data grid, you're already doing it wrong. Instead, you should manipulate data and let Binding and MVVM do their job. Listen, the exception you're getting is because your data template is empty, that's all.... add a control to it, so it can display your bid value. – jsanalytics Jan 06 '19 at 09:51
  • Like you said, I needed a control -- The answer is to apparently put a TextBlock into the cell in the DataTemplate which from someone who left WIndows Development back in the WinForms era is a statement that makes zero sense. It displays the data fine already, why would I need a control where the presumably already one doing the job fine. Why can't I refer to that? Anyway, I'll post the answer below. – JoeHz Jan 18 '19 at 08:26
  • _why would I need a control where the presumably already one doing the job fine?_ Because if you want to customize it, you have to replace with your own template. – jsanalytics Jan 19 '19 at 05:02
  • _Why can't I refer to that?_ You can... search online for how to iterate over the visual tree. However, it's not the proper way of doing it, you're going against a framework that's available to make things "easier" for you. It's the difference between properly opening your front door with the keys and kicking it down every time you need to get in...:O) – jsanalytics Jan 19 '19 at 05:02
  • Yes, I've gathered that. That was my "before" perspective. Humor me. I'm getting my feet wet with wpf here. – JoeHz Jan 19 '19 at 05:05

1 Answers1

-1

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>
JoeHz
  • 2,136
  • 1
  • 18
  • 29
  • The extra `BidText` column is completely unnecessary. You could have simply done something like this: `Background="{Binding Converter={StaticResource YourConverter}}"`. – jsanalytics Jan 19 '19 at 04:49
  • Actually no. BidText corresponds to the raw string data coming out of the database and BidValue is it being made output friendly. I didn't want to write a converter that was passed in data that had been i18n'd. I didn't even want BidText present in the grid (hence why it's Visibility=False) but I didn't see any other way to have it passed into a converter. If you've got a better way of skinning that cat, I'd be happy to talk offline. Like I said, I'm learning here. – JoeHz Jan 19 '19 at 05:13
  • If by "output friendly" you mean "formatted", then you should use the `StringFormat` parameter of your binding for that purpose. Again, you're creating a unnecessary redundancy. It's a OK you're learning... aren't we all? But if you're going to post an answer, people are going to comment on it... it's our duty...:O) – jsanalytics Jan 19 '19 at 05:45
  • I meant i18n'd. I didn't want to compare against N different languages in my converter. The comments need to be on the original topic I would think, less they muck with the signal to noise ratio. Finding out how do to what I did via google was hard enough as it was. – JoeHz Jan 19 '19 at 23:37