WPF - Change cell colour when operation is successfull

Tiago Silva 21 Reputation points
2021-01-12T11:02:45.52+00:00

Hello!

I have this datagrid:

55686-image.png

The last column has buttons ("+") that when pressed open the following window:

55724-image.png

In this window when the "Registar" button is pressed the app executes an operationa and returns to the previous window.

I want the colour of the "+" button in the previous window to turn green after that, how can I achieve that?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
766 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-01-12T16:41:13.96+00:00

    Hi Tiago,
    change in my code (in ViewModel):

    Public Sub Execute(parameter As Object) Implements ICommand.Execute  
      Dim d = TryCast(parameter, Data)  
      Dim wnd As New Window With {.Width = 200, .Height = 200}  
      Dim stp As New StackPanel  
      wnd.Content = stp  
      Dim btnOK As New Button With {.Content = "Registrar"}  
      Dim btnCancel As New Button With {.Content = "Cancelar"}  
      stp.Children.Add(btnCancel)  
      stp.Children.Add(btnOK)  
      AddHandler btnOK.Click, Sub()  
                                wnd.DialogResult = True  
                                wnd.Close()  
                              End Sub  
      AddHandler btnCancel.Click, Sub()  
                                    wnd.DialogResult = False  
                                    wnd.Close()  
                                  End Sub  
      If wnd.ShowDialog Then d.Executed = True  
    End Sub  
    

    Result:

    55804-x.gif

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-01-12T14:52:52.597+00:00

    Hi Tiago,
    try following demo:

    XAML:

    <Window x:Class="Window074"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp1.WpfApp074"  
            mc:Ignorable="d"  
            Title="Demo DataGrid with Plus" Height="450" Width="800">  
      <Window.Resources>  
        <local:ViewModel x:Key="vm"/>  
      </Window.Resources>  
      <Grid DataContext="{StaticResource vm}">  
        <DataGrid ItemsSource="{Binding View}"  AutoGenerateColumns="False">  
          <DataGrid.Resources>  
            <Style x:Key="bgButtonCell" TargetType="DataGridCell">  
              <Style.Triggers>  
                <DataTrigger Binding="{Binding Executed}" Value="True">  
                  <Setter Property="Background" Value="LightGreen"/>  
                </DataTrigger>  
              </Style.Triggers>  
            </Style>  
            <Style x:Key="bgButton" TargetType="Button">  
              <Setter Property="BorderBrush" Value="Transparent"/>  
              <Setter Property="Background" Value="White"/>  
              <Style.Triggers>  
                <DataTrigger Binding="{Binding Executed}" Value="True">  
                  <Setter Property="Background" Value="LightGreen"/>  
                </DataTrigger>  
              </Style.Triggers>  
            </Style>  
          </DataGrid.Resources>  
          <DataGrid.Columns>  
            <DataGridTextColumn Header="Col1" Binding="{Binding Col1}"/>  
            <DataGridTemplateColumn CellStyle="{StaticResource bgButtonCell}">  
              <DataGridTemplateColumn.CellTemplate>  
                <DataTemplate>  
                  <Button Content="+"   
                          Margin="5" VerticalAlignment="Center" HorizontalAlignment="Center"  
                          Command="{Binding Source={StaticResource vm}}"   
                          CommandParameter="{Binding}"  
                          Style="{StaticResource bgButton}"/>  
                </DataTemplate>  
              </DataGridTemplateColumn.CellTemplate>  
            </DataGridTemplateColumn>  
          </DataGrid.Columns>  
        </DataGrid>  
      </Grid>  
    </Window>  
    

    And classes:

    To show code is denied, see attached file: 55801-x.txt

    Result:

    55729-x.gif

    1 person found this answer helpful.