2015-07-10 133 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

這個關鍵問題嗎? –