Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one dataGrid which contain one column for red or green color image. I do trigger to set red or green color image if status is ERROR or OK which gate from database. I just want to change color for hole row if there is red image.

I do XAML code to set Red of Green Image as below
HTML
<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Name="IsReadImage" Source="read.png"/>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsRead}" Value="False">
                    <Setter TargetName="IsReadImage" Property="Source" Value="unread.png"/>
                </DataTrigger>             
            </DataTemplate.Triggers>         
        </DataTemplate>     
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn>


And I tried XAML code to change row background color as below
HTML
<Style TargetType="DataGridRow">
     <Style.Triggers>
         <DataTrigger Binding="{Binding Active}" Value="False">
             <Setter Property="Background" Value="Red" />
         </DataTrigger>
     </Style.Triggers>
 </Style>


But It did not change background color. Please help me
Posted
Updated 17-Feb-15 1:21am
v2

1 solution

You can achieve this behaviour by a so-called value converter:

This is just an example of how you could set colours
depending on values.

ValueToColorConverter.cs
C#
using System;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApplication12
{
    class ValueToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if(value.GetType() == typeof(bool))
            {
                if((bool)value)
                {
                    return Brushes.Green;
                }
                else
                {
                    return Brushes.Red;
                }
            }
            else
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}


MainWindow.xaml:
C#
<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication12"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <local:ValueToColorConverter x:Key="V2C"></local:ValueToColorConverter>
    </Window.Resources>
    
    <Grid>
        <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid Background="{Binding IsRead,Converter={StaticResource V2C}}"></Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


MainWindow.xaml.cs:
C#
using System.Windows;

namespace WpfApplication12
{
    public partial class MainWindow : Window
    {
        ViewModel vm;

        public MainWindow()
        {
            this.Loaded += MainWindow_Loaded;

            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            vm = new ViewModel();

            this.DataContext = vm;
        }
    }
}


ViewModel.cs:
C#
using System.Collections.ObjectModel;

namespace WpfApplication12
{
    class ViewModel : ViewModelBase
    {
        public ViewModel()
        {
            items = new ObservableCollection<item>();

            Items.Add(new Item() { IsRead = true });
            Items.Add(new Item() { IsRead = false });
            Items.Add(new Item() { IsRead = true });
            Items.Add(new Item() { IsRead = false });
            Items.Add(new Item() { IsRead = true });
        }

        private ObservableCollection<item> items;

        public ObservableCollection<item> Items 
        { 
            get
            {
                return items;
            }
            set 
            { 
                if (items != value)
                {
                    items = value;

                    OnPropertyChanged();
                }
            }
        }
    }
}


ViewModelBase.cs:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication12
{
    class ViewModelBase : INotifyPropertyChanged
    {
        [AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
        public sealed class CallerMemberNameAttribute : Attribute { }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        public void OnPropertyChanged([CallerMemberName]string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}


Item.cs:
C#
namespace WpfApplication12
{
    class Item
    {
        public bool IsRead { get; set; }
    }
}
 
Share this answer
 
v5
Comments
Dipika Wani 19-Feb-15 1:49am    
how can inherit viewModelBase class in both class?
Is it some mistake or I did not understand
TheRealSteveJudge 19-Feb-15 2:42am    
You're right!
Apologise for any confusion.
I mistakenly copied the wrong source code.
Please see updated solution.
Dipika Wani 19-Feb-15 3:29am    
ohh.. Thank you so much. Is it use for static data. Because I get dynamic data from database
TheRealSteveJudge 19-Feb-15 3:43am    
You're welcome!
Dipika Wani 19-Feb-15 4:14am    
Please Give u solution for dynamic data which get from database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900