根据datagrid中的选定记录填充文本框

时间:2016-02-18 20:38:23

标签: c# wpf xaml mvvm datagrid

使用MVVM模式编写WPF应用程序。我有一个绑定到数据模型的数据网格,它为搜索项创建数据集合。我在datagrid中显示的一个字段是唯一的引用号。当我点击数据网格中的记录时,我想在一些文本框中填入关于使用该人的参考编号选择的人的额外数据。

这是我的datagrid的xaml:

        var search = dal.PersonalRepository.GetAll();

        if ( Regex.IsMatch(SearchString, @"^(\(?\d{3}?\)?)?[-\.\s]?\d{3}[-\.\s]?\d{4}$") )
            search = phoneSearch(search);
        else if (Regex.IsMatch(SearchString, @"^r?\d+$"))
            search = refNoSearch(search);
        else if (Regex.IsMatch(SearchString, @"^e?['C'||'c']\d+$"))
            search = empIdSearch(search);
        else if (Regex.IsMatch(SearchString, @"^(\w\d{2}|\wDC|O\w{2})(\w{4})$"))
            search = casCodeSearch(search);
        else
            search = nameSearch(search);

        if (!ShowSeparatedUsers)
            search = search.Where(x => x.DateOfSeparation == null);

        //        .Where(p => p.FirstName.Trim().Contains(_searchParam1) && p.LastName.Trim().Contains(_searchParam2)
        //                    ||
        //                    (p.FirstName.Trim().Contains(_searchParam2) && p.LastName.Trim().Contains(_searchParam1))


        UserSearchCollection = new ObservableCollection<User>(search.Select(x => new User {
            RefNo = x.RefNo,
            Voice = x.Voice.Trim(),
            FirstName = x.FirstName.Trim(),
            LastName = x.LastName.Trim(),
            PreferredFirstName = x.PreferredFirstName.Trim(),
            PreferredLastName = x.PreferredLastName.Trim(),
            DateOfSeparation = x.DateOfSeparation,
            EmployeeId = x.EmployeeId.Trim()
        }).ToList());
        IsSearched = true;

以下是从我的视图模型中搜索的代码             if(string.IsNullOrWhiteSpace(SearchString))                 返回;

public class User : ObservableObject
{
    public int RefNo { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Suffix { get; set; }
    public string PreferredLastName { get; set; }
    public string PreferredFirstName { get; set; }
    public string Exemptions { get; set; }
    public string Voice { get; set; }
    public DateTime? DateOfSeparation { get; set; }
    public string EmployeeId { get; set; }
    public string Caspar { get; set; }
    public string Exempt { get; set; }
    public string City { get; set; }
    public string Title { get; set; }
    public string Budcat { get; set; }
    public string Posnno { get; set; }
    public string Rptto { get; set; }
    public string Cascode { get; set; }
    public string Budorg { get; set; }
    public string NTE { get; set; }
    public string FullName {
        get {
            return Utils.FormatFullName(LastName, FirstName, Suffix, PreferredLastName, PreferredFirstName,
                Exemptions);
        }
    }
}

我的模型叫做User,这就是它的样子

 <TextBox Style="{StaticResource textboxStyleBlue}"  Grid.Row="1" Grid.Column="1" Width="200" IsReadOnly="True" />

这是我想在数据网格中选择此人时填充数据的其中一个文本框的示例。

{{1}}

1 个答案:

答案 0 :(得分:0)

您可以使用绑定到DataGrid.SelectedItem:

  1. 为DataGrid指定一个名称:x:Name =“grid”
  2. 设置TextBox Text = {Binding ElementName = grid,Path = SelectedItem.LastName}
  3. 我认为,这应该适合你。