1

I am creating an application to show a list of windows services not running. the problem is that those services should reflect any changes occurring, that is if a service starts, that service should be removed from the displayed list.

I used a ListView, here is the code:

<Window.Resources>
    <ObjectDataProvider ObjectType="{x:Type local:NotifiableServiceController}"
    MethodName="GetServices" x:Key="ManageServices">
    </ObjectDataProvider>
</Window.Resources>

<Grid>
    <ListView Name="lstViewServices" Width="Auto" ItemsSource="{Binding Source={StaticResource ManageServices}}"
                SelectionMode="Single">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="SoftOne Services" DisplayMemberBinding="{Binding Path=DisplayName}" />
                <GridViewColumn Header="Status">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Status}">
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

and the function getting the service List:

public static ObservableCollection<NotifiableServiceController> GetServices()
        {
            ObservableCollection<NotifiableServiceController> oaServices = new ObservableCollection<NotifiableServiceController>();

            //Retrieving the services starting with "SQL"
            foreach (ServiceController sc in ServiceController.GetServices().Where(p => p.DisplayName.StartsWith("SQL")))
            {
                oaServices.Add(new NotifiableServiceController(sc));
            }

            return oaServices;
        }

The NotifiableServiceController is being updated at time interval to refresh the status of the associated Windows service. But only the first-time retrieved services (from GetServices() function) will be refreshed.

That is all fine till now. I just need to have only stopped processes displayed in the Listview and I think that can be done using Style or Triggers in XAML? if yes, how?

Thanks for your time.

3 Answers 3

1

I think that you shoud use ListCollectioView, and filter it using Filter property. First, you have to load all the servicies, and than you can update it every N seconds using a Timer.Elapsed event.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that is a potential solution but setting the ListCollectionView would require me to change a lot of code, please find my answer to see how I solved it
1

The solution was to add a style to the ListView and targetting the ListViewItem as follow:

<ListView.Resources>
    <Style TargetType="ListViewItem">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Status}" Value="Running">
                <Setter Property="ListBoxItem.Visibility" Value="Hidden" />
                <Setter Property="Height" Value="0" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.Resources>

Depending of the Service Status, I can take set the visibility and the Height of the ListViewItem

Comments

0

Just remove the running services from the ObservableCollection<NotifiableServiceController> you bound your ListView against and add it back whenever a service stopped. If you want to keep a collection of all services, create an extra collection for that.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.