This is a migrated thread and some comments may be shown as answers.

Autocomplete Textbox in CelleditTemplate

4 Answers 59 Views
GridView
This is a migrated thread and some comments may be shown as answers.
AP
Top achievements
Rank 1
Veteran
AP asked on 03 Aug 2011, 12:28 PM
I want to have an autocomplete text box (or possibly a combo box) in the edit template of a RadGridView.  I think I know how to do this, however, I need to bind this control to an observable collection that is created when the application is started, and held as an application variable:-
I.e:-
  public partial class App : Application
    {
 public ObservableCollection<DiagnosisReference> DiagnosisLookups = new ObservableCollection<DiagnosisReference>();
 
}

This is populated from a ria services call, but as it's an unchanging reference list, it's a lot more efficient to store this in memory, than make calls to the database thousands of times a day.

Outside of the RadGrid, the autocomplete box works fine:-
<sdk:AutoCompleteBox x:Name="DiagComplete" Width="70" FilterMode="Custom" Margin="5,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" TabIndex="0" >
                               <sdk:AutoCompleteBox.ItemTemplate>
                                   <DataTemplate>
                                       <StackPanel Orientation="Horizontal">
                                           <TextBlock  Text="{Binding DiagnosisCode}" Margin="0,0,5,0"/>
                                           <TextBlock Text="{Binding Description}" FontSize="7" FontStyle="Italic" Foreground="Gray"/>
                                       </StackPanel>
                                   </DataTemplate>
                               </sdk:AutoCompleteBox.ItemTemplate>
                           </sdk:AutoCompleteBox>

The itemssource is set by the code:-
DiagComplete.ItemsSource = curApp.DiagnosisLookups;

However, I'm stuck on how to get this binding to work when the control is part of a CellEditTemplate in a RadGrid.
Can I still bind in code behind, or do I need to do it in XAML. Either way, I'm at a loss at how to do this.


4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Aug 2011, 10:30 AM
Hello,

 The DataContext inside grid cells and rows is actually your data item - you can access only your data item properties! You may need to use StaticResource to reference desired source for your templates. 

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
AP
Top achievements
Rank 1
Veteran
answered on 09 Aug 2011, 09:25 AM
I did try defining a static resource. But it didn't seem to work when populating data from code behind (as initially the data is pulled from RIA services, and there are too many records to hand-code into a XAML static resource - let alone the fact that it would be a major design flaw).

Any example on how to do this would be very useful, as I can't find any on the web.
0
Vlad
Telerik team
answered on 09 Aug 2011, 09:28 AM
Hello,

 You can create also a view model similar to this blog post with desired additional properties for your data items. 

Best wishes,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
AP
Top achievements
Rank 1
Veteran
answered on 09 Aug 2011, 09:52 AM
It's not a MVVM application, and I've no desire to make it one.  Also, the example doesn't seem to address the issue that this data is held in a collection in memory, not directly from RIA services.

I have a class:-
public class DiagnosisReference : INotifyPropertyChanged
   {
       public event PropertyChangedEventHandler PropertyChanged;
 
       public void NotifyPropertyChanged(string propertyName)
       {
 
           if (PropertyChanged != null)
           {
 
               PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
           }
       }
 
       private string _DiagnosisCode;
 
       public string DiagnosisCode
       {
           get
           {
               return _DiagnosisCode;
           }
           set
           {
               _DiagnosisCode = value;
               NotifyPropertyChanged("DiagnosisCode");
           }
       }
 
       private string _Description;
 
       public string Description
       {
           get
           {
               return _Description;
           }
           set
           {
               _Description = value;
               NotifyPropertyChanged("Description");
           }
       }
 
       public override string ToString()
       {
 
           return DiagnosisCode;
       }
   }

An observable collection is defined in App.xaml.cs
public ObservableCollection<DiagnosisReference> DiagnosisLookups = new ObservableCollection<DiagnosisReference>();

It is then populated by a call to RIA services. This collection is then used throughout the application to provide the diagnosis reference data, without re-querying the database.

My problem is how to bind this collection to a control in the RadGridview (autocompleteTextBox / combobox etc), it can't be done at the class level, as I only want to read the data from RIA services once in the application lifecycle - one of the reasons for using a SilverLight client (to reduce server load and move more processing to the client).

Tags
GridView
Asked by
AP
Top achievements
Rank 1
Veteran
Answers by
Vlad
Telerik team
AP
Top achievements
Rank 1
Veteran
Share this question
or