I've found no documentation on this functionality. It didn't work exactly as I expected, but I was able to cobble it together. Specifically, I expected the current item to be passed into the item argument of the StyleSelector, but it was not. Instead, I had to drill down into the container object to get at the DataItem.
So, here is an example of defining three styles, each representing a different icon. The intent is to display the correct icon in an unbound field, depending on a proprty of the domain object being displayed.
From the xaml inside the XamDataGrid:
<igDP:XamDataGrid.Resources> <Style x:Key="UnmappedIconStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <ContentControl Template="{StaticResource UnmappedIcon}" /> </ControlTemplate> </Setter.Value> </Setter> </Style>
<Style x:Key="MappedIconStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <ContentControl Template="{StaticResource MappedIcon}" /> </ControlTemplate> </Setter.Value> </Setter> </Style>
<Style x:Key="SuggestedIconStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <ContentControl Template="{StaticResource SuggestedIcon}" /> </ControlTemplate> </Setter.Value> </Setter> </Style>
<ui:MappableEntityIconStyleSelector x:Key="IconStyleSelector" UnmappedStyle="{StaticResource UnmappedIconStyle}" MappedStyle="{StaticResource MappedIconStyle}" SuggestedStyle="{StaticResource SuggestedIconStyle}" />
</igDP:XamDataGrid.Resources>
<igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout>
<igDP:FieldLayout.Fields>
<igDP:UnboundField Name="MapStatusIcon" > <igDP:Field.Settings> <igDP:FieldSettings AllowResize="false" CellValuePresenterStyleSelector="{StaticResource IconStyleSelector}" CellContentAlignment="ValueOnly" CellMaxWidth="18" CellMinWidth="18" /> </igDP:Field.Settings> </igDP:UnboundField>
And here is the StyleSelector:
using System.Windows;using System.Windows.Controls;using MappingTool.Domain;using Infragistics.Windows.DataPresenter;
namespace MappingTool.Presentation{ public class MappableEntityIconStyleSelector : StyleSelector {
public Style UnmappedStyle { get; set; } public Style MappedStyle { get; set; } public Style SuggestedStyle { get; set; }
/// <summary> /// Returns the style to display a mappable entity's icon with in the grids. /// </summary> /// <param name="item"></param> /// <param name="container"></param> /// <returns></returns> public override Style SelectStyle(object item, DependencyObject container) { Style style = UnmappedStyle;
CellValuePresenter cvp = container as CellValuePresenter; if (cvp != null) { MappableEntity entity = cvp.Record.DataItem as MappableEntity;
if (entity != null) { switch (entity.MapStatus) { case MappableEntity.MapStatusEnum.Mapped: style = this.MappedStyle; break;
case MappableEntity.MapStatusEnum.Suggested: style = this.SuggestedStyle; break; } } }
return style; }
}}
If anyone can shed any light on why the item is not being passed in, or of a better way to acocmplish this, I'd like to hear about it.
Ok, so I changed the UnboundField in this sample to Field, and renamed the field to correspond to a property on the domain object (MapStatus).
When I take action on the domain object that causes it's MapStatus value to change, my style selector is not being fired to change the icon.
I'm notifying of property change for the MapStatus, as well as the Mapped Master Id. The latter is updated in the grid to reflect the underlying change, but again, the icon doesn't change and my selector is never called.
Any ideas on how to trigger the update?
Thank you.
Hi,
I think using a StyleSelector in this situation is going against the grain. You can achieve this type of behavior by simply applying some triggers. I made a little demo, which uses some Foo objects with a Status property of type MyEnum. Here's the Field configuration for showing different color icons based on the Status value. The icons update when the Status property changes (Foo implements INotifyPropertyChanged):
<igDP:Field Name="Status"> <igDP:Field.Settings> <igDP:FieldSettings> <igDP:FieldSettings.CellValuePresenterStyle> <Style TargetType="{x:Type igDP:CellValuePresenter}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=DataItem.Status}" Value="{x:Static local:MyEnum.Value1}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Ellipse Width="10" Height="10" Fill="Red" /> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> <DataTrigger Binding="{Binding Path=DataItem.Status}" Value="{x:Static local:MyEnum.Value2}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Ellipse Width="10" Height="10" Fill="Green" /> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> <DataTrigger Binding="{Binding Path=DataItem.Status}" Value="{x:Static local:MyEnum.Value3}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Ellipse Width="10" Height="10" Fill="Blue" /> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </igDP:FieldSettings.CellValuePresenterStyle> </igDP:FieldSettings> </igDP:Field.Settings></igDP:Field>
Once again, you're spot-on. I discovered that the StyleSelector does not, by nature, get re-evaluated.
Your suggestion is exactly what I needed.
Thanks, josh.
I got many example in which I can compare value of Field on the basis of some known thing.
Our requirement is ….We want to compare value with first record of Field and on that basis we want to change backcolor of cell.
e.g.
Employee Class
We have List<Employee> binded to XamDataGrid.
Now..I Add two fields in XAmDataGrid.
1. Field – ID ..For all records ..If ID[0] ( ID element of Record[0] of List<Emplyee> ) then we want to change cell color of other ID field
2. Field – Name
Hello Barry,
Thank you for your post. I have been looking into it, but since I cannot be completely sure how your data is organized I am not able to conclude what can cause this, so could you please send an isolated sample project, where the issue is reproduced, so I can investigate it further for you.
Looking forward for your reply.
I have the exact same problem. I used your solution which uses the style triggers. I get the colors set the first time, but then when the values change, the trigger does not fire. The object implements INotifyProperty change, and the value of the enum gets displayed in the grid, and changes when the value of the object does. Any suggestions as to why this may be happening?