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>