Set the selected item of an XAM Datagrid programatically according to its name
Something along the lines of this.
private void SearchResult(s开发者_StackOverflowtring nameOfBean)
{
foreach (Record VARIABLE in mbeanDataGrid.Records)
{
if (VARIABLE.ToString().Contains(nameOfBean))
{
((VARIABLE as DataRecord).DataItem as Record).IsSelected = true;
}
}
}
However i know this syntax is wrong and im looking some advice! Pretty much to select the item (As if you had clicked on it) via code. According to its name.
you can select records with the following code (if you want select more than one record)
private void ShowSearchResult(string searchStr)
{
var recordsToSelect = new List<Record>();
foreach (Record rec in xamGrid.Records) {
var yourData = rec is DataRecord ? ((DataRecord)rec).DataItem as YourDataClass : null;
if (yourData != null && yourData.MatchWithSearchStr(searchStr)) {
recordsToSelect.Add(rec);
}
}
xamGrid.SelectedItems.Records.Clear();
// you need linq -> .ToArray()
xamGrid.SelectedItems.Records.AddRange(recordsToSelect.ToArray(), false, true);
}
or if you only want to activate and select a record then do this one
private void ShowSearchResult(string searchStr)
{
foreach (Record rec in xamGrid.Records) {
var yourData = rec is DataRecord ? ((DataRecord)rec).DataItem as YourDataClass : null;
if (yourData != null && yourData.MatchWithSearchStr(searchStr)) {
xamGrid.ActiveRecord = rec;
// don't know if you really need this
xamGrid.ActiveRecord.IsSelected = true;
break;
}
}
}
hope this helps
精彩评论