For holding View and ViewModel devided and not use CodeBehind for logic I would recommend to use a DataGrid and Binding to search for, so View would be like:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" Grid.Column="0"></TextBox>
<TextBlock Text="{Binding SearchResult}" Grid.Row="0" Grid.Column="1"></TextBlock>
<DataGrid ItemsSource="{Binding Entries}" Grid.Row="1" Grid.ColumnSpan="2">
</DataGrid>
</Grid>
So SearchText is what to search for and SearchResult is the result. Both are bound to the ViewModel. And Connected View Model contains DoSearchBay() for update the SearchResult field with the value of the current SearchText, code be like:
public class ViewModel: INotifyPropertyChanged
{
private String _searchText = "Bay?";
private String _searchResult = "N/A";
public ObservableCollection<MyDataObject> Entries { get; set; }
public string SearchText
{
get => _searchText;
set
{
if (value == _searchText) return;
_searchText = value;
DoSearchBay();
OnPropertyChanged();
}
}
private void DoSearchBay()
{
var sel = Entries.Select((dm, index) => new { index, dm.Bay}).FirstOrDefault(obj => obj.Bay.Equals(_searchText, StringComparison.OrdinalIgnoreCase)) ;
if (sel != null)
{
SearchResult = "Found in row " + sel.index;
}
else
{
SearchResult = "N/A";
}
}
public string SearchResult
{
get => _searchResult;
set
{
if (value == _searchResult) return;
_searchResult = value;
OnPropertyChanged();
}
}
public ViewModel()
{
//Create Fake values
Entries = new ObservableCollection<MyDataObject>();
Entries.Add(new MyDataObject() {Bay = "Bay1", Am9 = "value1-1", Am10 = "value1-2", Am11 = "value1-3" });
Entries.Add(new MyDataObject() { Bay = "Bay2", Am9 = "value2-1", Am10 = "value2-2", Am11 = "value2-3" });
Entries.Add(new MyDataObject() { Bay = "Bay3", Am9 = "value3-1", Am10 = "value1-2", Am11 = "value3-3" });
}
// ToDo Implement INotifyPropertyChanged...
}
The Model containing data my look like:
public class MyDataObject
{
public String Bay { get; set; }
public String Am9 { get; set; }
public String Am10 { get; set; }
public String Am11 { get; set; }
}