Reputation: 10030
I recently grabbed the project here: https://github.com/Nimgoble/WPFTextBoxAutoComplete it adds autocomplete behavior to TextBoxes in WPF.
You add this property to a TextBox for the AutoComplete behavior: behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding YourCollection}"
I'm trying to get the behavior to work with the TextBoxes in a DataGridTextColumn with no success. How do I add this property to the TextBox contained within the DataGridTextColumn?
Thanks!
Edit: Tried making a DataTemplate Column, still did not work.
<DataGridTemplateColumn Header="Test Stuff">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Maybe something if off on my DataGrid Binding? Here is the DataGrid:
<DataGrid ItemsSource="{Binding UsersList.Users}"
AutoGenerateColumns="False"
GridLinesVisibility="All"
FontSize="12"
Margin="0"
HorizontalAlignment="Center"
BorderThickness="0">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}"
BasedOn="{StaticResource MetroDataGridRow}">
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"
ClipboardContentBinding="{x:Null}"
behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems, RelativeSource={RelativeSource AncestorType=DataGrid}}"
Header="Name" />
<DataGridTextColumn Binding="{Binding ID}"
ClipboardContentBinding="{x:Null}"
Header="User ID" />
<DataGridCheckBoxColumn Binding="{Binding Valid}"
ElementStyle="{DynamicResource MetroDataGridCheckBox}"
ClipboardContentBinding="{x:Null}"
Header="Valid Name" />
<DataGridTemplateColumn Header="Test Stuff">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Upvotes: 0
Views: 4194
Reputation: 89
If someone is still interested in this topic, there is an simple way to bind autocomplete to datagridtextcolumn. Using AutoCompleteBehavior from https://github.com/Nimgoble/WPFTextBoxAutoComplete and using BindingProxy class from https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
...
xmlns:behaviors="clr-namespace:WPFTextBoxAutoComplete;assembly=WPFTextBoxAutoComplete"
...
<DataGrid Name="dataGrid">
<DataGrid.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header = "Target" Binding = "{Binding Target}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="behaviors:AutoCompleteBehavior.AutoCompleteItemsSource"
Value="{Binding Data, Source={StaticResource proxy}}" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
...
InitializeComponent();
BindingProxy bindingProxy = dataGrid.Resources["proxy"] as BindingProxy;
var list = new List<string>();
list.Add("abc");
list.Add("bcd");
bindingProxy.Data = list;
....
Upvotes: 0
Reputation: 1510
You should use the behavior, probably you are facing a problem with the DataContext of your row.
Follow this answer to update your behavior binding taking the DataContext from the DataGrid : Bind to DataContext Property from within DataGridColumn
Upvotes: 2