Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
3627
TIP: How to use CellValuePresenterStyle
posted

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.

Parents Reply Children
No Data