在WPF数据网格中输入提前搜索

时间:2015-07-10 22:18:37

标签: c# 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文件。

我们如何实现这一目标?

2 个答案:

答案 0 :(得分:0)

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

实现此功能非常简单。

将附加属性设置为所需的属性名称,它可以神奇地工作。

TextSearch.TextPath="First"

答案 1 :(得分:-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;
相关问题