我創建了一個簡單的wmi查詢,然後創建一個自定義集合。 我已經添加了一個過濾器,它從文本更改事件的文本框中獲取字符串。 它按預期工作,但有1個主要缺點,每次嘗試過濾它實際上都在執行查詢。我希望它做的是存儲集合localy並對該局部變量進行操作,而不是一遍又一遍地執行wmi查詢。 我已經嘗試了多路方式,但目前爲止沒有運氣。 任何幫助將appriciated! :)c#wpf商店DataGrid收藏本地和過濾與文本框
這裏是1路我想...
主要CS:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
namespace myfilter
{
public partial class FilteringSample : Window
{
public FilteringSample()
{
InitializeComponent();
myProcesses items = new myProcesses();
lvUsers.ItemsSource = items.GetProcesses;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
view.Filter = UserFilter;
}
private bool UserFilter(object item)
{
if (String.IsNullOrEmpty(txtFilter.Text))
return true;
else
return ((item as myProcess).Name.IndexOf(txtFilter.Text, StringComparison.OrdinalIgnoreCase) >= 0);
}
private void txtFilter_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
CollectionViewSource.GetDefaultView(lvUsers.ItemsSource).Refresh();
}
}
}
類CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
namespace myfilter
{
class myProcesses
{
public IEnumerable<myProcess> GetProcesses
{
get
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process");
ManagementObjectCollection processList = searcher.Get();
myProcess myproc;
foreach (ManagementObject obj in processList)
{
if (obj != null)
{
myproc = new myProcess();
try
{
myproc.Name = obj["Name"].ToString();
}
catch { }
try
{
myproc.ID = obj["ProcessId"].ToString();
}
catch { }
yield return myproc;
}
}
}
}
}
class myProcess
{
public string Name { get; set; }
public string ID { get; set; }
}
}
XAML:
<Window x:Class="myfilter.FilteringSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="FilteringSample" Height="200" Width="300">
<DockPanel Margin="10">
<TextBox DockPanel.Dock="Top" Margin="0,0,0,10" Name="txtFilter" TextChanged="txtFilter_TextChanged" />
<DataGrid Name="lvUsers">
</DataGrid>
</DockPanel>
</Window
>
我想類似的東西到數據集上的ASP。例如,您可以查詢SQL Server,但將表存儲在數據集上並對數據集執行操作... – 2014-09-11 19:13:38