This is a migrated thread and some comments may be shown as answers.

GridViewCell Background color binding issue

2 Answers 131 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Maciej Czerwiakowski
Top achievements
Rank 1
Maciej Czerwiakowski asked on 11 Dec 2009, 01:44 AM
Hello,

I would like to bind cell background color to cell value (via converter of course). However when I declare style in GridViewResource like this:

<Style TargetType="{x:Type telerik:GridViewCell}"
  <Setter Property="Background"
    <Setter.Value> 
      <SolidColorBrush Color="{Binding Converter={StaticResource decimalToColor}}" /> 
    </Setter.Value> 
   </Setter> 
</Style> 
which simply should return Brushes.Red.Color for value < 0 Green for value  > 0 and left color White when value is equal to 0. In debug mode I discovered that value passed to value converter is eqal to whole object (I'm binding ItemSource to collection of custom business objects) instead of being equal to object property value which is binded to column.

When I try to do something like this:

<telerik:GridViewDataColumn Width="45" DataType="{x:Type sys:Decimal}" Header="H1" DataMemberBinding="{Binding Path=H1, Mode=TwoWay}" DataFormatString="{}{0:n0}" IsGroupable="False" IsSortable="False" IsFilterable="False"
  <telerik:GridViewColumn.CellStyle> 
     <Style TargetType="{x:Type telerik:GridViewCell}"
       <Setter Property="Background"
         <Setter.Value> 
           <SolidColorBrush Color="{Binding Converter={StaticResource cellBGConverter}}" />
         </Setter.Value> 
       </Setter> 
    </Style> 
  </telerik:GridViewColumn.CellStyle> 
</telerik:GridViewDataColumn> 

it's even working, color is changed (value converter gets cell value), but I get an error:

"System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=H1; DataItem='DailyPosition' (HashCode=7712694); target element is 'SolidColorBrush' (HashCode=16353339); target property is 'Color' (type 'Color')
"
which cause low performance :(
Any ideas? How to get cell value in first style declaration?

Thanks in advance,
MC



2 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 16 Dec 2009, 04:07 PM
Hello Maciej,

We pass the whole data item as a DataContext in order to provide greater flexibility to the users - this mimics microsoft's DataGrid behavior.

To achieve the desired functionality in your case you should bind to the Value property of the cell using relative source binding with templated parent.


Hope this helps,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
srinivas nch
Top achievements
Rank 1
answered on 08 Apr 2010, 07:01 AM
Thanks to you both(Stefan Dobrev,Maciej). Really, my requirement fits your question and answers.

Stefan Dobrev, could you please give some sample code for your explanation.

Thanks.
Tags
GridView
Asked by
Maciej Czerwiakowski
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
srinivas nch
Top achievements
Rank 1
Share this question
or