Reputation: 199
Writing WPF application using MVVM pattern. I have a datagrid bound to data model which creates a collection of data for a search term. One of the fields I display in the datagrid is a unique reference number. When I click on a record in the datagrid i want to populate some text boxes with extra data about the person selected using the reference number of that person.
Here is the xaml for my datagrid:
<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" >
<DataGrid x:Name="dgPersonal" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding UserSearchCollection}" RowDetailsVisibilityMode="VisibleWhenSelected" IsReadOnly="True" IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridTextColumn Width="50" x:Name="RefNo" Binding="{Binding RefNo}" Visibility="Hidden" />
<DataGridTextColumn Header="Name" Width="125" x:Name="FullName" Binding="{Binding FullName, Mode=OneWay}" />
<DataGridTextColumn Header="Phone" Width="75" x:Name="Voice" Binding="{Binding Voice}" />
<DataGridTextColumn Header="Preferred First Name" Width="125" x:Name="FirstName" Binding="{Binding PreferredFirstName}" />
<DataGridTextColumn Header="Preferred Last Name" Width="125" x:Name="LastName" Binding="{Binding PreferredLastName}"/>
<DataGridTextColumn Header="DOS" Width="90" x:Name="DateOfSeparation" Binding="{Binding DateOfSeparation, StringFormat=M/d/yyyy}" SortMemberPath="DateOfSeparation" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Here is the code the does the search from my view Model if (string.IsNullOrWhiteSpace(SearchString)) return;
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;
My model is called User and this is what it looks like
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);
}
}
}
This is a sample of one of the textboxes I want to fill with data when the person is selected in the datagrid.
<TextBox Style="{StaticResource textboxStyleBlue}" Grid.Row="1" Grid.Column="1" Width="200" IsReadOnly="True" />
Upvotes: 0
Views: 963
Reputation: 384
You can use a binding to DataGrid.SelectedItem:
I think, that should work for you.
Upvotes: 1