Datagrid row should change the color

elxneq

New member
Joined
Mar 10, 2021
Messages
2
Programming Experience
Beginner
Hey guys i m very new here so sorry if this is not the right place to post it but i really dont know where else i'd post it.

I'm new at WPF and i don't know that much yet but i'm still learning.

so here is my issue
I want to make a simple financial manager witch takes the the content of the texboxes into my datagrid and this all works but now i wanna change the background of CellRows into red.

I have a few Textboxes and one of them is for numbers (txt_amountID) like 42$ and when i type -42$ it should change the background of the CellRow into red

Here is my XAML Code:

XAML COde:
<DataGrid x:Name="dtg_aus" Grid.ColumnSpan="2" AutoGenerateColumns="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" >


                <DataGrid.Columns>

                    <DataGridTextColumn Header="Name" Width="*" Binding="{Binding nameID}" />

                    <DataGridTextColumn Header="Runtime" Width="*" Binding="{Binding runtimeID}"/>

                    <DataGridTextColumn x:Name="dataGridTextColumn" Header="amount" Width="*" Binding="{Binding amountID}" />

                 <DataGridTemplateColumn>

                        <DataGridTemplateColumn.CellTemplate>

                            <DataTemplate>

                                <Button x:Name="btn_del" Click="btn_del_click">

                                    <Button.Background>

                                        <ImageBrush ImageSource="/icon_trash.png"/>

                                    </Button.Background>

                                </Button>

                            </DataTemplate>

                        </DataGridTemplateColumn.CellTemplate>

                    </DataGridTemplateColumn>

                </DataGrid.Columns>


/// Here i tryed it with binding but it doesnt worked that well, actually doesnt worked at all

       <DataGrid.CellStyle>

          <Style TargetType="DataGridCell">

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

<Style.Triggers>

<DataTrigger Binding="{Binding ElementName=txt_amountID, Path=IsNegative}" Value="True">

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

</DataTrigger>

</Style.Triggers>

                    </Style>

                </DataGrid.CellStyle>

            <Button x:Name="btn_take" Content="Take" Click="btn_take_Click"/>

            <TextBox x:Name="txt_nameID"/>

            <TextBox x:Name="txt_runtimeID"/>

            <TextBox x:Name="txt_amountID"/>

            </DataGrid>

And here is my Code behinde but i just want to make it with XMAL if its possible, if not then please help me to make a if-else solution


C# Code:
public partial class MainWindow : Window

{

    public MainWindow()

    {

        InitializeComponent();

    }

    public class Booking

    {

        public string nameID {get; set;}

        public string runtimeID {get; set;}

        public string amountID {get; set;}

    }

    private void btn_take_Click(object sender, RoutedEventArgs e)

    {

        Booking var = new Booking();

        var.nameID = txt_nameID.Text;

        var.runtimeID = txt_runtimeID.Text;

        var.amoutID = txt_aboutID.Text;

        dtg_aus.Items.Add(var);

    }
 
You'll have to expose a IsNegative property on your view model for that approach to work. If you don't want to go down that route, you'll need to implement a value converter. See StackOverflow answer below to give you a lead both approaches:
 
You'll have to expose a IsNegative property on your view model for that approach to work. If you don't want to go down that route, you'll need to implement a value converter. See StackOverflow answer below to give you a lead both approaches:

Hey Thanks for the quick anwser

i already saw that one but it doesnt worked with Datagrid the thread is just about to color the textbox.
I tested it out and the code changes just the textbox instead of the DatagridRow
 

Latest posts

Back
Top Bottom