Property Grid - change Cell Background based on Cell Value (MVVM)

Grids for WPF Forum

Posted 4 years ago by Peter Treier
Version: 19.1.0685
Avatar

Good Morning all

I need to change the Propertygrids Cell Background Color based on the Cells Value.

I got this Object bound to the DataObject of the Grid:

    public class LogItemProperty
    {
        public LogItemProperty() { }

        [Category("Entry")]
        [DisplayName("Id")]
        [Description("Id of the Log-Record")]
        public int Id { get; set; }

        [Category("Entry")]
        [DisplayName("Level")]
        [Description("Error Level of the Log-Record")]
        public string Level { get; set; }

...

The View Model looks like that

	private LogItemProperty _selectedLogItemProperty;
	public LogItemProperty SelectedLogItemProperty
	{
		get => _selectedLogItemProperty;
		set => _selectedLogItemProperty = value;
	}

The Property Grid is configured like this

                <grids:PropertyGrid x:Name="propGrid" IsSummaryVisible="True"
                                    gridseditors:BuiltinPropertyEditors.IsEnabled="False"
                                    CanClearDataObjectOnUnload="True"
                                    DataObject="{Binding Path=SelectedLogItemProperty}"  Grid.Column="0" Grid.Row="0" Grid.RowSpan="2"
                                    IsReadOnly="True" />

The Grid is filled with the correct Data!

Now I tried to create a Style (to be honest,my first one):

<Style  x:Key="LogItemPropertyGridRowStyle"
        TargetType="{x:Type grids:PropertyGridItem}">
        <Style.Triggers>

            <!-- Warn -->
            <DataTrigger
                Binding="{Binding Level, Mode=OneWay, RelativeSource={RelativeSource Self}, PresentationTraceSources.TraceLevel=High}"
                Value="WARN">
                <Setter
                    Property="Background"
                    Value="{StaticResource WarnLevelColor}" />
            </DataTrigger>

            <!-- Error -->
            <DataTrigger
                Binding="{Binding Level, Mode=OneWay, RelativeSource={RelativeSource Self}, PresentationTraceSources.TraceLevel=High}"
                Value="ERROR">
                <Setter
                    Property="Background"
                    Value="{StaticResource ErrorLevelColor}" />
            </DataTrigger>

            <!-- Fatal -->
            <DataTrigger
                Binding="{Binding Level, Mode=OneWay, RelativeSource={RelativeSource Self}, PresentationTraceSources.TraceLevel=High}"
                Value="FATAL">
                <Setter
                    Property="Background"
                    Value="{StaticResource FatalLevelColor}" />
            </DataTrigger>

        </Style.Triggers>
</Style>

But nothing happend :-(

Any help is welcome!

Kind Regards
Peter

Comments (3)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Peter,

First, are you trying to change all of the PropertyGrid cells' backgrounds based on the Level value, or just the Level cell's background?

Second, are you wanting to change all cell backgrounds, or name column only, or value column only in these scenarios?


Actipro Software Support

Posted 4 years ago by Peter Treier
Avatar

I just want to change the Cell bound to the property 'Level'...

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Peter,

In that case, I would make a custom property editor for the property named Level.  The DataTemplate for the PropertyEditor.ValueTemplate could have a Border as its outer element and you could set the Border.Background to a SolidColorBrush that was bound to the data context (Level value), but used a ValueConverter to convert the Level value to an appropriate brush.

Please see our "Grids / PropertyGrid Features / Property Editors" topic in the documentation that comes with the product for details on property editors and making a ValueTemplate.  Some QuickStarts also show it.


Actipro Software Support

The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.