-2

Good evening everyone, Excuse me for my bad english. I want to change the background color from negative or positive values but it should be more géneral in the future. Is it useless to give some link to look at because i've tryed some of them and i can't do them. Like NameToBrushConverter isn't compatible in wpf... Currently I have my datagrid into an application..

XAML code :

    <UserControl x:Name="AnUserControl" x:Class="WpfControls.MyDataGrid"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfControls"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" MouseDoubleClick="UserControl_MouseDoubleClick" PreviewKeyDown="UserControl_PreviewKeyDown" MouseDown="UserControl_MouseClick" >
<UserControl.Resources>

    <Style x:Key="GridHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="12" />
    </Style>

    <Style x:Key="GridStyle" TargetType="{x:Type DataGrid}" >
        <Setter Property="AutoGenerateColumns" Value="False" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="GridLinesVisibility" Value="None" />
        <Setter Property="CanUserAddRows" Value="False" />
        <Setter Property="CanUserDeleteRows" Value="False" />
        <Setter Property="CanUserResizeColumns" Value="True" />
        <Setter Property="CanUserResizeRows" Value="False" />
        <Setter Property="CanUserReorderColumns" Value="False" />
        <Setter Property="ColumnHeaderStyle" Value="{StaticResource GridHeaderStyle}" />
    </Style>
</UserControl.Resources>
<DataGrid ItemsSource="{Binding Path=Records}" Name="xxx"
    Style="{StaticResource GridStyle}" Margin="0,-4,0,4" >
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Delete" Click="MenuItemDelete_Click" Name="Delete"  />
            <MenuItem Header="Add" Click="MenuItemAdd_click" />
        </ContextMenu>
    </DataGrid.ContextMenu>
</DataGrid>

How I should be able to change the color I looked in multiple Stackoverflow but my knowledge in wpf isn't good.. I want to use binding, because it would be for multiple using... I have an userControl and other exemple talk about window ressources and i currently don't know how to implement this into my code But in fact i'm missing some informations about binding and i think it's the cause I don't get it. If someone can get me some informations that could be amazing ! :) Thanks for answering have a nice day :)

  • 5
    Possible duplicate of [Change DataGrid cell colour based on values](https://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values) – ASh Jun 01 '17 at 14:18
  • See this link : https://social.msdn.microsoft.com/Forums/vstudio/en-US/b1e715ef-c52c-457d-be9d-e56b207f5051/wpf-datagrid-change-cell-background-color-based-on-a-value-passed?forum=wpf or https://social.msdn.microsoft.com/Forums/vstudio/en-US/c91d895d-a2a8-40e0-af01-7b26a12919fc/wpf-datagrid-changing-cell-text-foreground-color-based-on-positive-or-negative-value?forum=wpf – PaulF Jun 01 '17 at 14:26

1 Answers1

0

this morning with a brain clean, I have resolved it I forget to chose to chose namespace and build it.. Hope it's helps for begginner