C# 使用文本框筛选WPF数据网格
我正在制作这个应用程序,它使用XML节点从Sharepoint站点提取数据C# 使用文本框筛选WPF数据网格,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在制作这个应用程序,它使用XML节点从Sharepoint站点提取数据 private XmlNode GetListItems(string listTitle) { var client = new Bluejeanware.MWELS.Lists(); System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "passw
private XmlNode GetListItems(string listTitle)
{
var client = new Bluejeanware.MWELS.Lists();
System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
client.Credentials = passCredentials;
return client.GetListItems(listTitle, string.Empty, null, null, string.Empty, null, null);
}
public void BindSPDataSource()
{
var data = GetListItems("Tasks");
var result = XElement.Parse(data.OuterXml);
XNamespace z = "#RowsetSchema";
var taskItems = from r in result.Descendants(z + "row")
select new
{
TaskName = r.Attribute("ows_LinkTitle").Value,
DueDate = r.Attribute("ows_DueDate") != null ? r.Attribute("ows_DueDate").Value : string.Empty,
AssignedTo = r.Attribute("ows_AssignedTo") != null ? r.Attribute("ows_AssignedTo").Value : string.Empty,
};
dataGridView.ItemsSource = taskItems;
}
我想用一个文本框过滤数据,这篇文章就是一个很好的例子
我很难将这段代码转换成与我的应用程序将数据添加到Datagrid的方式兼容的方式,有什么想法吗?-首先定义一个TaskItem类来保存OuterXml中每个节点的属性
-如果DataContext设置为,请确保ViewModel或codebehind实现INorifyPropertyChanged接口,以将属性中的更改传播到UI
-然后将taskItems转换为TaskItem类的ObservableCollection属性,并定义绑定到filter文本框的filter属性
这是完整的代码
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
//Populate the TaskItems collection using BindSPDataSource() method
}
private String _filter = String.Empty;
public String Filter
{
get
{
return _filter;
}
set
{
if (_filter == value)
{
return;
}
_filter = value;
OnPropertyChanged();
TaskItems = new ObservableCollection<TaskItem>(TaskItems.Where(x => x.AssignedTo.ToLower().Contains(_filter) ||
x.DueDate.ToLower().Contains(_filter) ||
x.TaskName.ToLower().Contains(_filter)
));
}
}
private ObservableCollection<TaskItem> _taskItem;
public ObservableCollection<TaskItem> TaskItems
{
get
{
return _taskItem;
}
set
{
if (_taskItem == value)
{
return;
}
_taskItem = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class TaskItem
{
public String TaskName { get; set; }
public String DueDate { get; set; }
public String AssignedTo { get; set; }
}
最后,在构造函数中,不要直接填充DataGrid ItemSource,请在BindSPDataSource方法中更改:
dataGridView.ItemsSource=taskItems;
对这样的事情:
TaskItems=new ObservableCollection(taskItems);
以下示例演示了如何: 通过SharePoint Web Services从SharePoint检索列表数据 绑定到DataGrid并启用筛选 XAML: 代码隐藏:
namespace SPO
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
ListItemCollection = CollectionViewSource.GetDefaultView(LoadTasks());
ListItemCollection.Filter = FilterTask;
}
public bool FilterTask(object value)
{
var entry = value as TaskEntry;
if (entry != null)
{
if (!string.IsNullOrEmpty(_filterString))
{
return entry.TaskName.Contains(_filterString);
}
return true;
}
return false;
}
/// <summary>
/// Bind SP Data Source
/// </summary>
private IEnumerable<TaskEntry> LoadTasks()
{
var data = GetListItems("http://intranet.contoso.com","Tasks");
var result = XElement.Parse(data.OuterXml);
XNamespace z = "#RowsetSchema";
var taskItems = from r in result.Descendants(z + "row")
select new TaskEntry
{
TaskName = r.Attribute("ows_LinkTitle").Value,
DueDate = r.Attribute("ows_DueDate") != null ? r.Attribute("ows_DueDate").Value : string.Empty,
AssignedTo = r.Attribute("ows_AssignedTo") != null ? r.Attribute("ows_AssignedTo").Value : string.Empty,
};
return taskItems;
}
private XmlNode GetListItems(string webUri,string listTitle)
{
var client = new Lists.Lists();
client.Url = webUri + "/_vti_bin/Lists.asmx";
return client.GetListItems(listTitle, string.Empty, null, null, string.Empty, null, null);
}
public ICollectionView ListItemCollection
{
get { return _listItemCollection; }
set { _listItemCollection = value; NotifyPropertyChanged("ListItemCollection"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public string FilterString
{
get { return _filterString; }
set
{
_filterString = value;
NotifyPropertyChanged("FilterString");
if (_listItemCollection != null)
{
_listItemCollection.Refresh();
}
}
}
private ICollectionView _listItemCollection;
private string _filterString;
}
public class TaskEntry
{
public string TaskName { get; set; }
public string DueDate { get; set; }
public string AssignedTo { get; set; }
}
}
结果
TaskItems=new ObservableCollection(taskItems);
<Window x:Class="SPO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Tasks" Width="800px" Height="600px" Name="TasksWindow">
<StackPanel DataContext="{Binding ElementName=TasksWindow}">
<TextBox Text="{Binding FilterString, UpdateSourceTrigger=PropertyChanged}" />
<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding ListItemCollection}" />
</StackPanel>
</Window>
namespace SPO
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
ListItemCollection = CollectionViewSource.GetDefaultView(LoadTasks());
ListItemCollection.Filter = FilterTask;
}
public bool FilterTask(object value)
{
var entry = value as TaskEntry;
if (entry != null)
{
if (!string.IsNullOrEmpty(_filterString))
{
return entry.TaskName.Contains(_filterString);
}
return true;
}
return false;
}
/// <summary>
/// Bind SP Data Source
/// </summary>
private IEnumerable<TaskEntry> LoadTasks()
{
var data = GetListItems("http://intranet.contoso.com","Tasks");
var result = XElement.Parse(data.OuterXml);
XNamespace z = "#RowsetSchema";
var taskItems = from r in result.Descendants(z + "row")
select new TaskEntry
{
TaskName = r.Attribute("ows_LinkTitle").Value,
DueDate = r.Attribute("ows_DueDate") != null ? r.Attribute("ows_DueDate").Value : string.Empty,
AssignedTo = r.Attribute("ows_AssignedTo") != null ? r.Attribute("ows_AssignedTo").Value : string.Empty,
};
return taskItems;
}
private XmlNode GetListItems(string webUri,string listTitle)
{
var client = new Lists.Lists();
client.Url = webUri + "/_vti_bin/Lists.asmx";
return client.GetListItems(listTitle, string.Empty, null, null, string.Empty, null, null);
}
public ICollectionView ListItemCollection
{
get { return _listItemCollection; }
set { _listItemCollection = value; NotifyPropertyChanged("ListItemCollection"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public string FilterString
{
get { return _filterString; }
set
{
_filterString = value;
NotifyPropertyChanged("FilterString");
if (_listItemCollection != null)
{
_listItemCollection.Refresh();
}
}
}
private ICollectionView _listItemCollection;
private string _filterString;
}
public class TaskEntry
{
public string TaskName { get; set; }
public string DueDate { get; set; }
public string AssignedTo { get; set; }
}
}