Locked WPF listbox sreach with data template

  • Friday, March 30, 2012 4:59 AM
     
     

    Hello ,

       I am trying to filer the data in the wpf  listbox based on the input given .

     Currently I am able to do if I have only one field in the template , what if I have multiple fields.

    I am writing a textchanged in the TextBox function

        private void ListSearch_TextChanged(object sender, TextChangedEventArgs e)
            {
                string txtOrig = txtNameToSearch.Text;
                string upper = txtOrig.ToUpper();
                string lower = txtOrig.ToLower();


                var filteration= from test in lstEmployee
                                  let ename = test .firstname
                                  where ename.StartsWith(upper) || ename.StartsWith(lower) || ename.Contains(txtOrig)
                                  select test ;
             
                lstEmpData.ItemsSource = filteration;
            }

    My Xaml file

    <ListBox>

      <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding firstname}"></TextBlock>                                          
                        </StackPanel>                                     
                    </DataTemplate>               
                </ListBox.ItemTemplate>

    <ListBox>

     <TextBox Height="27" HorizontalAlignment="Left"
    Name="ListSearch" TextChanged="ListSearch_TextChanged" />

    SO I type in some name and the listbox shows me the filtered data with the name visible , [this is only for  one text block]

      Now the above one works fine .

    ============================

     If I want to add a new textblock

    <TextBlock Text="{Binding Lastname}"></TextBlock>

    How do I process this second content Lastname? So  now the list box contains a template containing firstname and Lastname,

    IF I type the name the reulsts are fine but number , it doesnt show up. Even If I type the number , I need to get the list item  to be shown [which inturn contains firstname and Lastname].. I am assuming something needs to be done in the Linq query.[if  my assumption is correct]

     and when I just change the query to Last name  without adding extra textblock , I get a null referece exception unhandeled error  (Object reference not set to an instance of an object.)

    Could you please provide me some light how do I go about this?

    Thank You