23 Answers, 1 is accepted
Why not use CellStyleSelector instead? Please check this demo for more info.
All the best,Vlad
the Telerik team
In the SelectStyle method of the StyleSelector you have reference to the entire object (data item) and you can access any property to perform any calculation. You can check also MSDN and my blog post for more info about style selectors.
Regards,Vlad
the Telerik team
| <Style x:Key="LateDueOnDockStyle" TargetType="telerikPresentation:GridViewCell"> |
| <Setter Property="Background" Value="Red" /> |
| </Style> |
| <Style x:Key="NotLateDueOnDockStyle" TargetType="telerikPresentation:GridViewCell" > |
| <!--<Setter Property="Background" Value="Green" />--> |
| </Style> |
| <local:DueOnDockConveter x:Key="converter" /> |
| <local:ConditionalStyleSelector x:Key="DueOnDockSelector" ConditionConverter="{StaticResource converter}"> |
| <local:ConditionalStyleSelector.Rules> |
| <local:ConditionalStyleRule Style="{StaticResource LateDueOnDockStyle}"> |
| <local:ConditionalStyleRule.Value> |
| <sys:Boolean>True</sys:Boolean> |
| </local:ConditionalStyleRule.Value> |
| </local:ConditionalStyleRule> |
| <local:ConditionalStyleRule Style="{StaticResource NotLateDueOnDockStyle}"> |
| <local:ConditionalStyleRule.Value> |
| <sys:Boolean>False</sys:Boolean> |
| </local:ConditionalStyleRule.Value> |
| </local:ConditionalStyleRule> |
| </local:ConditionalStyleSelector.Rules> |
| </local:ConditionalStyleSelector> |
| <telerikGridView:RadGridView x:Name="radgridRouteView" ShowColumnFooters="False" RowIndicatorVisibility="Collapsed" Grid.Row="1" |
| AutoGenerateColumns="False" ShowGroupPanel="False" IsFilteringAllowed="False" |
| IsReadOnly="True" EnableRowVirtualization="True" |
| SelectionChanged="routeView_SelectionChanged" Style="{StaticResource DefaultRowStyle}"> |
| <telerikGridView:RadGridView.SortDescriptors> |
| <telerikData:SortDescriptor Member="Route" SortDirection="Ascending" /> |
| </telerikGridView:RadGridView.SortDescriptors> |
| <telerikGridView:RadGridView.Columns> |
| <telerikGridView:GridViewDataColumn Header="Route" HeaderTextAlignment="Center" UniqueName="Route" TextAlignment="Center" IsReadOnly="True" Width="100" /> |
| <telerikGridView:GridViewDataColumn Header="Trailer" HeaderTextAlignment="Center" UniqueName="TrailerNumber" TextAlignment="Center" IsReadOnly="True" Width="70" /> |
| <telerikGridView:GridViewDataColumn Header="Due On Dock" HeaderTextAlignment="Center" UniqueName="DueOnDock" TextAlignment="Center" IsReadOnly="True" DataFormatString="{}{0:HH:mm}" CellStyleSelector="{StaticResource DueOnDockSelector}" /> |
| </telerikGridView:RadGridView.Columns> |
| <telerikNavigation:RadContextMenu.ContextMenu> |
| <telerikNavigation:RadContextMenu Opened="RouteStopRadContextMenu_Opened" ItemClick="RouteStopRadContextMenu_ItemClick" > |
| <telerikNavigation:RadMenuItem x:Name="receiveRoute" Header="Receive Route" Icon="Images/delivery.png" /> |
| <telerikNavigation:RadMenuItem x:Name="undoReceiveRoute" Header="Set Route Pending" Icon="Images/Edit_UndoHS.png" /> |
| <telerikNavigation:RadMenuItem x:Name="noRoute" Header="Route Not Valid For Today" /> |
| <telerikNavigation:RadMenuItem IsSeparator="True" /> |
| <telerikNavigation:RadMenuItem x:Name="refreshRouteView" Header="Refresh Route View" Icon="Images/RefreshDocViewHS.png" /> |
| </telerikNavigation:RadContextMenu> |
| </telerikNavigation:RadContextMenu.ContextMenu> |
| </telerikGridView:RadGridView> |
| using System.Windows.Data; |
| using CAXDock_RiaServices.Web; |
| using System.Collections.Generic; |
| using System; |
| using System.Linq; |
| using System.Text; |
| using System.Windows; |
| using System.Windows.Media; |
| #if !SILVERLIGHT |
| using System.Windows.Controls; |
| #else |
| using Telerik.Windows.Controls; |
| #endif |
| namespace CAXDock_RiaServices |
| { |
| enum RouteStates |
| { |
| NotReceived, |
| Received, |
| NoStopForToday |
| } |
| public class DueOnDockConveter : IValueConverter |
| { |
| #region IValueConverter Members |
| public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
| { |
| DailyRoutePM routeInfo = value as DailyRoutePM; |
| if (routeInfo != null) |
| { |
| if (routeInfo.RouteState.Value == (int)RouteStates.NotReceived) |
| { |
| return routeInfo.DueOnDock < DateTime.Now ? true : false; |
| } |
| } |
| return null; |
| } |
| public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
| { |
| throw new NotImplementedException(); |
| } |
| #endregion |
| } |
| public class ConditionalStyleSelector : StyleSelector |
| { |
| public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container) |
| { |
| object conditionValue = this.ConditionConverter.Convert(item, null, null, null); |
| foreach (ConditionalStyleRule rule in this.Rules) |
| { |
| if (Equals(rule.Value, conditionValue)) |
| { |
| return rule.Style; |
| } |
| } |
| return base.SelectStyle(item, container); |
| } |
| List<ConditionalStyleRule> _Rules; |
| public List<ConditionalStyleRule> Rules |
| { |
| get |
| { |
| if (this._Rules == null) |
| { |
| this._Rules = new List<ConditionalStyleRule>(); |
| } |
| return this._Rules; |
| } |
| } |
| IValueConverter _ConditionConverter; |
| public IValueConverter ConditionConverter |
| { |
| get |
| { |
| return this._ConditionConverter; |
| } |
| set |
| { |
| this._ConditionConverter = value; |
| } |
| } |
| } |
| public class ConditionalStyleRule |
| { |
| object _Value; |
| public object Value |
| { |
| get |
| { |
| return this._Value; |
| } |
| set |
| { |
| this._Value = value; |
| } |
| } |
| Style _Style; |
| public Style Style |
| { |
| get |
| { |
| return this._Style; |
| } |
| set |
| { |
| this._Style = value; |
| } |
| } |
| } |
| } |
Hum, that seems very strange. Is it convenient for you to send us the whole application so that we can debug it on our site?
Regards,
Milan
the Telerik team
Attaching projects in the forums is not allowed! Please open support ticket.
All the best,Vlad
the Telerik team
you can pass actually any value by using this attribute.
im also using PriceToColorConverter class provided in the Telerik conditional formating example to apply the formating.
hope this help.
private void RadGrid_PrdSummary_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) { if (e.Row is GridViewRow) { System.Windows.Data.Binding CellFgBinding = new System.Windows.Data.Binding("UnHr"); CellFgBinding.Converter = new PrdMonitorSL3.Web.PriceToColorConverter(); bool isGreaterThanRate = (int.Parse(e.Row.Cells[3].Content.ToString()) < int.Parse(e.Row.Cells[5].Content.ToString())); CellFgBinding.ConverterParameter = isGreaterThanRate; e.Row.Cells.Last().SetBinding(GridViewCellBase.ForegroundProperty, CellFgBinding); } }I'm having the same issue using a CellStyleSelector.
Thanks - Ben
Can you post more info about the problem? You can check our demo here (rows virtualization in RadGridView is enabled by default).
Kind regards,Vlad
the Telerik team
I went back to using the RowLoaded event and everything works fine.
My problem was I was saying:
if ( negative_value )
color = red;
I added:
else
color = black;
and everything works fine.
Thanks again - Ben
I use CellStyleSelector as it is in the sample. But when I update the value on which the actual style depends, cell gets the style only after scrolling out of the screen and back. How can I force the gridview to apply a new style immediately?
Thanks,
Juliana
We have modified the behavior of CellStyleSelector to update the style of the data object raised a PropertyChanged event for the property the column is bound to. You should be able to find the new functionality in this week's Internal Build, which will be available for download later today.
Greetings,
Yavor Georgiev
the Telerik team
I replaced all dlls with v. 2010.2.1008.1040. But behavior remained the same. Should I change anything in code?
Thanks,
Juliana
Can you verify if your object implements INotifyPropertyChanged properly?
Greetings,Vlad
the Telerik team
Actually neither ConditionalStyleSelector nor my Converter implements INotifyPropertyChanged. Could you provide an example, so that I could if everything is right?
Thanks,
Juliana
You do not need INotifyPropertyChanged for this - just for the data items in your collection.
Kind regards,Vlad
the Telerik team
I have binding in grid ItemsSource="{Binding CateringList}" and this property in my viewmodel
private List<EMS.DtoLibrary.Catering> _CateringList;
public List<EMS.DtoLibrary.Catering> CateringList
{
get
{
return _CateringList;
}
set
{
_CateringList = value;
NotifyPropertyChanged("CateringList");
}
}
Juliana
Can you verify if EMS.DtoLibrary.Catering implements INotifyPropertyChanged?
All the best,Vlad
the Telerik team
Now I see what you mean :) Yes, this class implements INotifyPropertyChanged. And when I change value in a cell, this particular cell repaints. But it's not enough in my case. The thing is that cell style depends on another cell value. So I changed value in one cell, and another cell should change its color. How can I force that cell to repaint?
Thanks,
Juliana
Please find attached a sample solution. It uses the our latest internal build binaries, which have this functionality. The CellStyleSelector will be invoked every time the property of the column is mentioned in a PropertyChanged event. Thus if the style of the cell depends on another property, make sure you raise two PropertyChanged events, just like I have done in the Established property of the Club class.
Sincerely yours,
Yavor Georgiev
the Telerik team
Thanks for the sample. That is definitely what I need to put everything on its place.
Thank you both for the help.
Best Regards,
Juliana
