0

I would like to change the Background, FontWeight, Foreground in dxg:GridColumn. I wrote this code but unfortunately it does not work. How can I fix it?

public class CellViewModel {
    public string Value { get; set; }
    public string FontWeight { get; set; }
    public string Foreground { get; set; }
    public string Background { get; set; }
}

public class TestItemViewModel {
    public CellViewModel Column1 { get; set; }
    public CellViewModel Column2 { get; set; }
    public CellViewModel Column3 { get; set; }
}

public class TestViewModel {
    public TestViewModel() {
        this.Items = new ObservableCollection<TestItemViewModel> {
            new TestItemViewModel {
                Column1 = new CellViewModel { Value = "CCC", FontWeight = "Bold", Foreground = "Red", Background = "Green" }, 
                Column2 = new CellViewModel { Value = "-683,84", FontWeight = "Normal", Foreground = "Red", Background = "SeaGreen" }, 
                Column3 = new CellViewModel { Value = "-683,84", FontWeight = "Normal", Foreground = "Red", Background = "SeaGreen" }, 
            }
        };
    }
    public ObservableCollection<TestItemViewModel> Items { get; set; }
}

<dxg:GridControl HorizontalContentAlignment="Right"
                 AutoPopulateColumns="False"
                 ItemsSource="{Binding Path=Performance.Items}">
    <dxg:GridControl.Resources>
        <DataTemplate x:Key="GridColumnHorizontalContentAlignmentTemplate">
            <dxe:TextEdit x:Name="PART_Editor" HorizontalContentAlignment="Right" />
        </DataTemplate>
    </dxg:GridControl.Resources>
    <dxg:GridControl.Columns>
        <dxg:GridColumn AllowColumnFiltering="False"
                        CellTemplate="{StaticResource GridColumnHorizontalContentAlignmentTemplate}"
                        FieldName="BuyAndHold.Value"
                        Header="Column 1"
                        HorizontalHeaderContentAlignment="Right">
            <dxg:GridColumn.CellStyle>
                <Style TargetType="dxg:CellContentPresenter">
                    <Setter Property="Background" Value="{Binding Path=Column1.Background}" />
                    <Setter Property="FontWeight" Value="{Binding Path=Column1.FontWeight}" />
                    <Setter Property="Foreground" Value="{Binding Path=Column1.Foreground}" />
                </Style>
            </dxg:GridColumn.CellStyle>
        </dxg:GridColumn>
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
         <dxg:TableView AllowEditing="False"
                        AllowGrouping="False"
                        AllowSorting="False"
                        NavigationStyle="None"
                        PrintTotalSummary="False"
                        ShowGroupPanel="False"
                        ShowHorizontalLines="False"
                        ShowIndicator="False"
                        ShowTotalSummary="False"
                        ShowVerticalLines="False" />
    </dxg:GridControl.View>
</dxg:GridControl>

I can not understand why there is

<Style TargetType="dxg:CellContentPresenter">
    <Setter Property="Background" Value="{Binding Path=BuyAndHold.Background}" />
    <Setter Property="FontWeight" Value="{Binding Path=BuyAndHold.FontWeight}" />
    <Setter Property="Foreground" Value="{Binding Path=BuyAndHold.Foreground}" />
</Style>

no data binding?

If I set the value manually everything works. For example.

<Setter Property="Background" Value="Red" />

Work Fine.

DmitryG
  • 17,677
  • 1
  • 30
  • 53

1 Answers1

0

The binding's Path you are using is wrong.
Use the following syntax:

<Setter Property="Background" Value="{Binding Path=RowData.Row.BuyAndHold.Background}"/>

instead of:

<Setter Property="Background" Value="{Binding Path=BuyAndHold.Background}"/>

Related Example: How to Conditionally Apply Styles (CellStyle)

DmitryG
  • 17,677
  • 1
  • 30
  • 53