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.