C# WPF:隐藏使用GridviewColumn定义的Listview项

C# WPF:隐藏使用GridviewColumn定义的Listview项,c#,wpf,xaml,C#,Wpf,Xaml,我正在创建一个应用程序以显示未运行的windows服务列表。问题在于,这些服务应该反映发生的任何更改,也就是说,如果服务启动,该服务应该从显示的列表中删除 我使用了ListView,下面是代码: <Window.Resources> <ObjectDataProvider ObjectType="{x:Type local:NotifiableServiceController}" MethodName="GetServices" x:Key="ManageSe

我正在创建一个应用程序以显示未运行的windows服务列表。问题在于,这些服务应该反映发生的任何更改,也就是说,如果服务启动,该服务应该从显示的列表中删除

我使用了ListView,下面是代码:

<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>

以及获取服务列表的函数:

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;
        }
publicstaticobserveCollection GetServices()
{
ObservableCollection oaServices=新的ObservableCollection();
//检索以“SQL”开头的服务
foreach(ServiceController.GetServices()中的ServiceController sc,其中(p=>p.DisplayName.StartsWith(“SQL”))
{
oaServices.Add(新的NotifiableServiceController(sc));
}
返回OAS服务;
}
正在按时间间隔更新NotifiableServiceController,以刷新关联Windows服务的状态。但是只有第一次检索到的服务(从
GetServices()
函数)将被刷新

到现在为止一切都很好我只需要停止Listview中显示的进程,我认为可以使用XAML中的样式或触发器来完成吗?如果是,如何进行


谢谢您的时间。

只要从您绑定的
列表视图的
可观察集合中删除正在运行的服务,并在服务停止时将其添加回即可。如果您想保留所有服务的集合,请为此创建一个额外的集合。

我认为您应该使用ListCollectionView,并使用filter属性对其进行过滤。首先,您必须加载所有服务,然后可以使用计时器每N秒更新一次。已用事件。

解决方案是向ListView添加样式,并将ListViewItem作为目标,如下所示:

<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>


根据服务
状态
,我可以设置ListViewItem的可见性和高度

谢谢,这是一个潜在的解决方案,但是设置ListCollectionView需要我更改很多代码,请找到我的答案,看看我是如何解决的