Search code examples
c#wpfautocompletedatagridtextbox

How can i find my WPF Autocompletebox in my DataGrid?


Hello I've been trying to integrate an Autocompletebox into my project for days, unfortunately without success. I'm getting a little desperate. I have at least found a very simple solution for a text box outside of my Datagrid (DotNetProjects.WpfToolkit.Input 6.0.90 https://www.nuget.org/packages/DotNetProjects.WpfToolkit.Input/) but I can't put it in my . I followed the instructions in this video: https://www.youtube.com/watch?v=SK8TXvcPWqI. I will post my Code here:

<Grid>
<controls:AutoCompleteBox x:Name="ACB2" Text="TT" ></controls:AutoCompleteBox> 
</Grid>

This Box is working. I Can find the Box "ACB2" in my Code:

public Databasewindow()                                                  
        {
            InitializeComponent();
            ACB2.FilterMode = AutoCompleteFilterMode.Contains;
            ACB2.ItemsSource = new string[] { "TEST1", "TEST2 TEST2", "TEST3 TEST3" };

        }

But when i try to fill a Column and use this box i fail and cant find my Textbox anymore

<Grid>
<DataGrid x:Name="DataGrid1"
    <DataGridTemplateColumn  Width="200" Header=" Command ">
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <controls:AutoCompleteBox 
                                Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                                x:Name="ACB2" 
                                ></controls:AutoCompleteBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
           </DataGrid>
         </Grid>

The Binding Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" is working good. But i cant find the x:Name="ACB2" in my Code anymore. Can anyone help me with this? I tried many different AutoCompleteFunctions (Nuget) but i always had problems with the suggestions-Bindings. Maybe someone has some experience with this and can can help me out? That would be very, very nice. Kind regards


Solution

  • So i found a solution for my Binding problem. Thank you Melkor V for your support. Here is my Code:

    In my Xaml i had to add these lines for my Binding:

    XAML:

    xmlns:controls='clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Input.Toolkit' xmlns:local="clr-namespace:Databases"
    

    In my Datagrid => Commandselection is the Class where i wrote a List and AutoCompleteBoxItems is the Name of the List

    <DataGrid 
       <DataGrid.DataContext>
             <local:Commandselection/>
       </DataGrid.DataContext>
    </DataGrid>
    
    <DataGridTemplateColumn  Width="200" Header=" Command ">
                        <DataGridTemplateColumn.CellTemplate >
                            <DataTemplate>
                                <controls:AutoCompleteBox 
                                    Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                                    ItemsSource="{Binding Path = DataContext.AutoCompleteBoxItems, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                    FilterMode="Contains" 
                                    ></controls:AutoCompleteBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    

    Here is the Code of my Class:

    class Commandselection 
        {
    
            public static ObservableCollection<string> AutoCompleteBoxItems { get; set; }
            public Commandselection()
            {
                AutoCompleteBoxItems = new ObservableCollection<string>()
                {
                    "Apple",
                    "Banana",
                    "Carrot",
                    "Dog",
                    "Elderberry",
                    "Fruit",
                    "Grapes",
                    "Honey",
                    "Iron"
                };
            }
            
        }