There's a nice tutorial (made by Telerik) that shows how to use CellTemplateSelectors to make cells look differently depending on the value of the column-bound property.
So for the CellTemplate, the example from the link is using this DataTemplate:
<
DataTemplate
x:Key
=
"zHighValueTemplate"
>
<
TextBlock
ForeGround
=
"Red"
Text
=
"{Binding bigint}"
/>
</
DataTemplate
>
The problem is that this Template uses the specific property (namely bigint) bound to the TextBlock.Text.
That's great if I'd have to modify only a single column.
However, I have a class that contains several integer properties which I want to show in GridView.
For example:
public
class
PersonScore
{
string
Name {
get
;
set
; }
int
MaxScore {
get
;
set
; }
int
LastScore {
get
;
set
; }
int
IntervalScore {
get
;
set
; }
}
I have these two DataTemplates:
<
DataTemplate
x:Key
=
"scoringPositive"
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
ForeGround
=
"Green"
Text
=
"{Binding MaxScore, Converter={StaticResource scoringConverter}}"
/>
<
Image
Source
=
"/Scoring;component/Images/checkmark.png"
Visibility
=
"{Binding MaxScore, Converter={StaticResource scoreToVisibilityConverter}}"
Margin
=
"10,0,0,0"
/>
</
StackPanel
>
</
DataTemplate
>
<
DataTemplate
x:Key
=
"scoringNonPositive"
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
ForeGround
=
"Red"
Text
=
"{Binding MaxScore, Converter={StaticResource scoringConverter}}"
/>
<
Image
Source
=
"/Scoring;component/Images/exclamation.png"
Visibility
=
"{Binding MaxScore, Converter={StaticResource scoreToVisibilityConverter}}"
Margin
=
"10,0,0,0"
/>
</
StackPanel
>
</
DataTemplate
>
So basically, if the MaxScore value is lower than 0, the cellTemplateSelector would chose the scoringNonPositive template, the TextBlock ForeGround would thus be Red and the Text set to whatever comes out of the scoringConverter. Additionaly, if the MaxScore is lower than 100, the Image's visibility would be set to Visible by the scoreToVisibilityConverter so that the exclamation mark is visible next to the score.
In other scenario, with MaxScore greater than or equal to 0, the text would be Green and checkmark image visible if MaxScore greater than 100, etc.
This all works as intended.
However, I have more than one property bound to the column. In worst-case scenario, I'd have to create dataTemplates for each specific property in my class (MaxScore, LastScore, IntervalScore) and I guess such repetition would be bad practice.
Is there any way to write the DataTemplate so that it can choose the appropriate Property, depending on which column it is bound to? That means that I want to be able to create only 2 DataTemplates, since all of them behave the same, only the source property is different.
(The example in this post is a dummy example, my real class has about 10 such properties, so it would require writing 20 DataTemplates which I'm trying to avoid here).