views:

332

answers:

1

Hi all, my problem is here: I have some class

public class Component
{
    ...
    private ServiceController service;

    ...
    public int ServiceStatus
    {
        get
        {
            switch(service.Status)
            {
                case ServiceControllerStatus.Stopped:
                    return 0;

                case ServiceControllerStatus.Running:
                    return 1;

                default:
                    return 2;
            }
        }
    }
    public void QueryService()
    {
        service.Refresh();
    }
}

and collection of Components, declared in another class:

public class Motivation
{
    // Downloaded data
    ...
    private ObservableCollection<Component> components;
    public ObservableCollection<Component> Components
    {
        get { return components; }
    }

    public bool CheckServices()
    {
        bool changed = false;
        foreach (Component C in components)
        {
            int prevStatus = C.ServiceStatus;
            C.QueryService();

            if (prevStatus != C.ServiceStatus)
                changed = true;
        }

        return changed;
    }

This components list displayed in WPF DataGrid. My idea: green background color for running services, red - for stopped. Works fine, but only on start. CheckServices() called by timer, and if returned value is True, i want to rerender my grid, respect to new service statuses. Here is XAML:

<Style x:Key="ServiceStateStyle" TargetType="z:DataGridRow">
        <Setter Property="Background" Value="Gray" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=ServiceStatus}" Value="0">
                <Setter Property="Background" Value="LightCoral" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ServiceStatus}" Value="1">
                <Setter Property="Background" Value="LightGreen" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <z:DataGrid Grid.Row="0"
                Grid.ColumnSpan="4"
                AutoGenerateColumns="False"
                x:Name="DataGridComponents"
                ItemContainerStyle="{DynamicResource ServiceStateStyle}">
                <z:DataGrid.Columns>
                        <z:DataGridTextColumn IsReadOnly="True"
                            Header="Component" Width="80"
                            Binding="{Binding Path=DisplayName}"/>
                    </z:DataGrid.Columns>
                </z:DataGrid>

Should i call any method explicit to invalidate DataGrid? I have tried with InvalidateProperty, InvalidateVisual, GetBindingExpression(ItemContainerStyleProperty).UpdateTarget(), but nothing work. Can anyone help?

A: 

The Component class must implement the INotifyPropertyChanged and raise the event when some of it's property change.

ema
Its work! Great and simple. Thank you.
Pavel