0

I have a problem integrating the AutoComplete function into my DataGridView. When I customize a cell, I want to open a list of suggestions. Similar to the Google search bar. I've tried a few things that I've found on the internet, but without success. I downloaded a package from https://github.com/Nimgoble/WPFTextBoxAutoComplete with the AutoCompleteFunction, which already exists in WinForms but not in WPF. Here is my Code:

<DataGridTextColumn Width="200" Header=" Column1 " Binding="{Binding Column1}">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Setter Property="MaxLength" Value="50"></Setter>
            <Setter Property="behaviors:AutoCompleteBehavior.AutoCompleteItemsSource" Value="{Binding ???, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
            <EventSetter Event="TextChanged" Handler="TextBox_TextChanged">
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

I want to add a new class to my Project with a List (strings) with all Suggestions I need. Maybe someone has an Idea? I tried to copy Using WPF TextBox Autocomplete in a DataGrid but I failed.

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
Mario Allw
  • 13
  • 5
  • Pls see https://github.com/quicoli/WPF-AutoComplete-TextBox – Mansur Kurtov Jul 24 '21 at 09:48
  • Hello Mansur Kurtov, i tried to use your example but i dont know how to bind a List of Strings to the AutoCompleteTextBox. Can you help me? I will post my actual code here. I am new to coding and WPF and i really have problems to understand those Bindings in WPF. – Mario Allw Jul 28 '21 at 15:26

2 Answers2

0

This is my actual Code:

<DataGridTemplateColumn  Width="200" Header=" Command ">
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <wpf:AutoCompleteTextBox 
                                MaxLength="50" Text="{Binding Command, Mode=TwoWay}" 
                                Provider="{Binding suggestions}"
                                />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

The Binding Command is working without problems.

public interface ISuggestionProvider
        {
            IEnumerable GetSuggestions(string filter);
        }

I just copied that from the example https://www.codeproject.com/Tips/801004/WPF-AutoComplete-TextBox


public DatabaseWindow()                                                 
       
 {
            InitializeComponent();

            var provider = new SuggestionProvider(x =>
            {
                IEnumerable suggestions;
                suggestions = "TEST1";
                return suggestions;
            });

 }
Mario Allw
  • 13
  • 5
0

For Binding string collection to AutoCompleteTextBox (in your case) you may use:

<wpf:AutoCompleteTextBox x:Name="MyTextBox"
                          Text="{Binding Command, Mode=TwoWay}"/>

...

          var provider = new SuggestionProvider(x =>
            {
                var suggestions =new List<string>() { "TEST1","TEST2","TEST3" };
                return suggestions;
            });
           MyTextBox.Provider = provider;
Mansur Kurtov
  • 726
  • 1
  • 4
  • 12
  • Thanks for your support. Your code worked but i needed something else. I found a solution in another thread. https://stackoverflow.com/questions/68573535/how-can-i-find-my-wpf-autocompletebox-in-my-datagrid/68586273#68586273 – Mario Allw Jul 30 '21 at 06:19