0

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>
Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128

2 Answers2

2

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

Community
  • 1
  • 1
Giangregorio
  • 1,482
  • 2
  • 11
  • 12
  • The TextColumn cells already have successful data binding to a string property. Is this unrelated to that? – Douglas Gaskell Aug 30 '15 at 07:22
  • The string property you are binding the textcolumn and the AutoCompleteItemsSource are member of the same object? – Giangregorio Aug 30 '15 at 07:34
  • They are. The same object has both. I did test this with a TextBox using the same ViewModel (and having the TextBox bound to a string along with the collection for the auto-complete) and the autocomplete did function. Is there someplace I need to put this property other than in the DataGridTextColumn itself? – Douglas Gaskell Aug 30 '15 at 08:14
  • I did try what the post you linked to suggested as well, I also just added an edit with another thing I attempted. – Douglas Gaskell Aug 30 '15 at 09:02
  • 1
    UsersList.Users has a property called TestItems? – Giangregorio Aug 30 '15 at 09:23
  • 1
    I just realized that!! After posting that it came to my attention that I need to reference the DataContext my UserControl is using and not the one my DataGrid was using. Thankfully it's easy since I'm using a StaticResource till I can figure out how to properly use DymanicResources between windows and UserControls. – Douglas Gaskell Aug 30 '15 at 09:25
0

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;
....
Community
  • 1
  • 1