0

I have a textbox and what's typed in there must be searched in the datagrid.

Picture in the link for clarity. https://www.dropbox.com/s/8s98bes1g54xjaz/Search.png

I use the MVVM model.

XAML:
Textbox:

    <TextBox x:Name="txtZoeken" HorizontalAlignment="Center" VerticalAlignment="Center" Height="29" Grid.Row="0" TextWrapping="Wrap" Width="238" FontSize="20" Text="Zoeken..."/>

Datagrid:
    <DataGrid ItemsSource="{Binding Contactpersons}" AutoGenerateColumns="False" x:Name="PersonenGrid" Grid.Column="1" Margin="35,99,8,10" Grid.Row="5" Grid.ColumnSpan="3">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <cmd:EventToCommand Command="{Binding EditCommand}" PassEventArgsToCommand="True"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <DataGrid.Columns >
                    <DataGridTextColumn Header="ID"  Binding="{Binding ID}"/>
                    <DataGridTextColumn Header="Naam"  Binding="{Binding Name}"/>
                    <DataGridTextColumn Header="Bedrijf" Binding="{Binding Company}" />
                    <DataGridTextColumn Header="Functie" IsReadOnly="True" Binding="{Binding JobRole/Name}"/>
                    <DataGridTextColumn Header="Stad" Binding="{Binding City}" />
                    <DataGridTextColumn Header="E-mail" Binding="{Binding Email}" />
                    <DataGridTextColumn Header="Telefoon" Binding="{Binding Phone}" />
                    <DataGridTextColumn Header="GSM" Binding="{Binding Cellphone}" />
                </DataGrid.Columns>
            </DataGrid>
user2827958
  • 357
  • 1
  • 4
  • 20
  • What are you looking to do with matching item(s)? For instance, Do you wish to limit your collection to show only the matching or to select the first matching item? – weeksdev Dec 01 '13 at 16:42
  • Anything is good really. Select the one that resembles closest or something. – user2827958 Dec 01 '13 at 16:51
  • If you are looking to multi select (want to fully control selections through your VMs) take a look at this http://stackoverflow.com/questions/2615271/wpf-datagrid-multiselect-binding on how to Bind a row selections to their respective VMs. – atomaras Dec 01 '13 at 22:15
  • Nono, It has to selected the row that matches the closest search. I already have the match but now I want it to automatically select the row – user2827958 Dec 02 '13 at 09:38

1 Answers1

3

You can create an additional property within the ViewModel called SelectedPerson

        private Person_selectedperson;

        public Person SelectedPerson
        {
            get { return _selectedperson; }
            set
            {
                _selectedperson = value;
                OnPropertyChanged("SelectedPerson");
            }
        }

This property should represent one instance within the collection of Contactpersons. Then, bind this property to SelectedItem within the datagrid

<DataGrid ItemsSource="{Binding Contactpersons}" SelectedItem="{Binding SelectedPerson}"

Then within your event/method related to the textbox search button. Run a linq query or something similar to find the first match within the collection and set SelectedPerson to that item (I did it within a search click event while quickly writing the code, however, you may want to place in command in viewmodel to better adhere to mvvm)...

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string searchValue = textbox1.Text;
            vm.SelectedPerson = vm.Contactpersons.Where(a => a.LastName.Contains(searchValue)).FirstOrDefault();
        }

There will be additional error handling required beyond this example (checking if there is no matches etc)

weeksdev
  • 4,265
  • 21
  • 36