How to binding API data to WPF windows form

zleug 56 Reputation points
2020-11-03T06:09:10.71+00:00

Hi All.
I have WPF project that need use API to search according record by entered value in the TextBox of the form. I know how to manipulate data if form binding to SQL Server database. But I don't have experience to work with API. I will appreciate for sample and detail explanation how to use API in WPF project .

Thanks.

Developer technologies | Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,651 Reputation points Moderator
    2020-11-04T02:47:14.77+00:00

    To implement the display of relevant record by inputing value , you can use AutoCompleteBox.Here is my steps to do it.
    Step 1: Search and install DotNetProjects.Input.Toolkit in your project Nuget Package
    Step 2: Add xmlns:tookit="clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Input.Toolkit" to import the dll
    Step 3: The code for xaml:

     <Grid>  
            <tookit:AutoCompleteBox x:Name="searchTextBox"  ValueMemberPath="SerchString" Margin="10" FontSize="20" Height="50" Foreground="Black">  
                <tookit:AutoCompleteBox.ItemTemplate>  
                    <DataTemplate>  
                        <TextBlock Margin="5,5" FontSize="26">  
                            <Run Text="{Binding SerchString}" Foreground="Blue"/>  
                            <Run Text="{Binding Name}" Foreground="Gray"/>  
                        </TextBlock>  
                    </DataTemplate>  
                </tookit:AutoCompleteBox.ItemTemplate>  
            </tookit:AutoCompleteBox>  
        </Grid>  
    

    Step 4: Code for cs:

      public MainWindow()  
            {  
                InitializeComponent();  
                this.searchTextBox.Populating += new PopulatingEventHandler(AutoCompleteBox_Populating);  
            }  
      
            private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e)  
            {  
                e.Cancel = true;  
                List<AutoCompleteModel> data = new List<AutoCompleteModel>();  
                for (int i = 0; i < 10; i++)  
                {  
                    AutoCompleteModel model = new AutoCompleteModel();  
                    Random radom = new Random();  
                    model.SerchString = radom.Next(1,30).ToString();  
                    model.Name = "TestName" + i;  
                    data.Add(model);  
                }  
                this.searchTextBox.ItemsSource = data;  
                this.searchTextBox.FilterMode = AutoCompleteFilterMode.Contains;  
                this.searchTextBox.PopulateComplete();  
            }  
    

    The code for AutoCompleteModel:

      public class AutoCompleteModel : INotifyPropertyChanged  
        {  
            public void OnPropertyChanged(string propname)  
            {  
                if (this.PropertyChanged != null)  
                {  
                    PropertyChanged(this, new PropertyChangedEventArgs(propname));  
                }  
            }  
      
      
            public event PropertyChangedEventHandler PropertyChanged;  
            private string searchString = string.Empty;  
            private string name = string.Empty;  
      
            public string SerchString  
            {  
                get { return searchString; }  
                set  
                {  
                    searchString = value;  
                    this.OnPropertyChanged("SerchString");  
                }  
            }  
      
      
            public string Name  
            {  
                get { return name; }  
                set  
                {  
                    name = value;  
                    this.OnPropertyChanged("Name");  
                }  
            }  
        }  
    

    Step 5: The result is like below shown:
    37342-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.