开发者

Colouring a hierarchical XamDataGrid

开发者 https://www.devze.com 2023-04-06 10:25 出处:网络
I\'m using a XamDataGrid (Infragistics-control) to display some hierarchical data. The objects that I can have up to 10 levels and I need to be able to give each level a specific background-color. I u

I'm using a XamDataGrid (Infragistics-control) to display some hierarchical data. The objects that I can have up to 10 levels and I need to be able to give each level a specific background-color. I use 开发者_运维百科the AssigningFieldLayoutToItem-event to get the "level" of the item and it would be best to assign the background/style here as well, I suppose.

I have tried specifying a DataRecordCellArea-style and even a CellValuePresenter-style but I can't get any of these to work with the FieldLayouts.

Another solution is to write a FieldLayout for each level, but this would create a lot of unnecessary XAML-code.

Any suggestions as to what I should do?


If you have a different FieldLayout for each level, you could use a single style targeting the DataRecordPresenter with a converter to set the background.

XAML:

<local:BackgroundConverter x:Key="BackgroundConverter"/>
<Style TargetType="{x:Type igDP:DataRecordPresenter}">
    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=FieldLayout.Key, Converter={StaticResource BackgroundConverter}}"/>
</Style>

Converter:

    public class BackgroundConverter:IValueConverter
{
    public BackgroundConverter()
    {
        this.Brushes = new Dictionary<string, Brush>();
    }

    public Dictionary<string, Brush> Brushes {get;set;}
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is string)
        {
            string key = value.ToString();
            if (this.Brushes.ContainsKey(key))
                return this.Brushes[value.ToString()];
        }                
        return Binding.DoNothing;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The following will set the colors to use for fields with Key1 and Key2:

BackgroundConverter backgroundConverter = this.Resources["BackgroundConverter"] as BackgroundConverter;
backgroundConverter.Brushes.Add("Key1", Brushes.Green);
backgroundConverter.Brushes.Add("Key2", Brushes.Yellow);

If you are reusing the same FieldLayout for multiple fields, then you could use the InitializeRecord event and change the style to bind to the Tag of the DataRecord like this:

XAML:

    <Style TargetType="{x:Type igDP:DataRecordPresenter}">
    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Record.Tag}"/>
</Style>

C#:

    void XamDataGrid1_InitializeRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e)
{
    if (!e.ReInitialize)
    {
        // Set the tag to the desired brush.
        e.Record.Tag = Brushes.Blue;
    }
}

Note that I didn't add the conditional logic for determining the brush to use and that still needs to be done for different levels to have different backgrounds.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号