2015-07-10 84 views
-2

您好我有一个网格与学生的集合型WPF数据网格向前搜索

public class Student 
{ 
public string First{get;set;} 
public string Last{get;set;} 
public int Age{get;set;} 
} 

MyGrid.ItemsSource= new List<Student>(){new Student{First="First1",Last="Last1",Age=1}, 
new Student{First="First2",Last="Last2",Age=2}}, 
new Student{First="First3",Last="Last3",Age=3}}, 
new Student{First="First4",Last="Last4",Age=4}},}; 

现在的页面或网格被加载后,用户只需输入一些字符,而应用程序中选择特定的一行将用户输入与姓氏或名字匹配。

这与我们在Windows资源管理器或Visual Studio中看到的完全相似,只需键入以在Win Explorer中选择文件夹或在visualstudio中选择.cs文件即可。

我们如何实现这一目标?

回答

0

经过两天的研究,我终于找到答案。

实现此功能非常简单。

将附加属性设置为期望的属性名称,并且它神奇地工作。

TextSearch.TextPath="First" 
+0

此附加属性适用于任何ItemsControl,如Listbox,DataGrid .... 我不知道为什么这是张贴的,但它是一个有效的问题 – WPFKK

-1

DataGrid情况如下:

<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding Key}" Header="Key"/> 
       <DataGridTextColumn Binding="{Binding Value}" Header="Value"/> 
      </DataGrid.Columns> 
     </DataGrid> 

如果你想要像Key X Value你不必为此创建model,该.NET Framework 4.5已经为这个特定的类,类Dictionary <TKey, TValue>TKey是密钥数据的类型和TValue是值的数据的类型,所以就需要有C#代码等(或等于)以下:

Dictionary<string, string> dictionary = new Dictionary<string, string>() 
{ 
    {"Key1","Value1"}, 
    {"Key2","Value2"}, 
    {"Key3","Value3"} 
}; 

this.myDataGrid.ItemsSource = dictionary; 
+0

请理解我的问题。这是关键时搜索不是关于如何分配datagrid数据源 – WPFKK

+0

这个关键问题吗? –