Search code examples
c#wpfxamlautocompletewpfdatagrid

Using WPF TextBox Autocomplete in a DataGrid


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>

Solution

  • 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