1

I have a Silverlight DataGrid bound to a collection of MyObjects. MyObject has a boolean field called IsHighlighted. I would like to change the row's background color when this value is true. And to have it changed back if it becomes false.

I already tried by using the Loading_Rowevent (as explained here), but it didn't work for me, as this event is only called once, and my objetcs all have the boolean value set to false at this time (it only becomes truc when another component is selectes; this works, I checked the values).

Anybody has a clue ? Thanks in advance !

Update: I made a test application to illustrate, it reproduces my problem.

<navigation:Page x:Class="AViews.Tests" 
           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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           DataContext="{Binding RelativeSource={RelativeSource Self}}"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Tests Page">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <sdk:DataGrid Grid.Row="0" ItemsSource="{Binding AllItems, Mode=TwoWay}" AutoGenerateColumns="False" LoadingRow="DataGrid_LoadingRow">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Binding="{Binding Value1}" Header="Value1" />
                <sdk:DataGridTextColumn Binding="{Binding Value2}" Header="Value2"/>
                <sdk:DataGridCheckBoxColumn Binding="{Binding IsHighlighted}" Header="Is Highlighted" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

        <Button Content="Change !" Grid.Row="1" HorizontalAlignment="Left" Click="Button_Click" />

    </Grid>
</navigation:Page>

public partial class Tests : Page, INotifyPropertyChanged
{
    private SampleConverter bgConverter = new SampleConverter();
    Random r = new Random();

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    private ObservableCollection<Sample> allItemsField = new ObservableCollection<Sample>();
    public ObservableCollection<Sample> AllItems
    {
        get
        {
            return this.allItemsField;
        }

        set
        {
            if (this.allItemsField != value)
            {
                this.allItemsField = value;
                this.OnPropertyChanged("AllItems");
            }
        }
    }

    public Tests()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var tmp = Enumerable.Range(0, 100).Select(f => new Sample(f)).ToList();
        foreach (var item in tmp)
        {
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
        }

        var coll = new ObservableCollection<Sample>(tmp);

        this.AllItems = coll;
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        this.OnPropertyChanged("AllItems");
    }

    private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        Binding b = new Binding("IsHighlighted")
        {
            Mode = BindingMode.OneWay,
            Converter = this.bgConverter,
            ValidatesOnExceptions = true
        };

        e.Row.SetBinding(DataGridRow.BackgroundProperty, b);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        foreach (var item in this.AllItems)
        {
            item.IsHighlighted = r.Next(1000) % 2 == 0;
        }
    }
}

public class Sample: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    private string value1Field = string.Empty;
    public string Value1
    {
        get
        {
            return this.value1Field;
        }

        set
        {
            if (this.value1Field != value)
            {
                this.value1Field = value;
                this.OnPropertyChanged("Value1");
            }
        }
    }

    private string value2Field = string.Empty;
    public string Value2
    {
        get
        {
            return this.value2Field;
        }

        set
        {
            if (this.value2Field != value)
            {
                this.value2Field = value;
                this.OnPropertyChanged("Value2");
            }
        }
    }

    private bool isHighlightedField = false;
    public bool IsHighlighted
    {
        get
        {
            return this.isHighlightedField;
        }

        set
        {
            if (this.isHighlightedField != value)
            {
                this.isHighlightedField = value;
                this.OnPropertyChanged("IsHighlighted");
            }
        }
    }

    public Sample(int index)
    {
        this.Value1 = string.Format("Value1 #{0}", index);
        this.Value2 = string.Format("Value2 #{0}", index);
    }
}

public class SampleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool val = (bool)value;
        SolidColorBrush ret = val ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Green);
        return ret;
    }

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

And the result can be seen on those pictures:

When I first arrive on the page. Start

I click the button, which sets some (random) values to true. As you can see, the binding is updated, not the UI. enter image description here

I use the scrollbar, go to the end, and come back, and Oh! wonderful! All the rows are correctly colored :-(

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.