Don W.
Don W.

Reputation: 560

WPF AutoCompleteBox multiple fields

I have a WPF autocompletebox and it is populated with a list of Organization_Names queried from database. Now I want to get the Organization_ID when a user selects an organization name from the autocompletebox dropdown. I can query the database again to get the ID based on the selected Organization_Name, but I think there got be a better way to do. How to add an invisible column in the dropdown so it won't be displayed but i can get its value? I am pretty new to WPF. Any help would be appreciated.

Thanks, Alex

Upvotes: 1

Views: 1158

Answers (1)

codeBlue
codeBlue

Reputation: 195

You can bind the autocomplete box to an object.

class MyClass
{
    public int Organization_ID{ get; set; }
    public string Organization_Names{ get; set; }
}

<controls:AutoCompleteBox x:Name="autoCompleteBox1"    
      SelectionChanged="autoCompleteBox1_SelectionChanged"      
      FilterMode="Contains"              
      IsTextCompletionEnabled="True">
    <controls:AutoCompleteBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Organization_Names}" />
        </DataTemplate>
    </controls:AutoCompleteBox.ItemTemplate>
</controls:AutoCompleteBox>

private void autoCompleteBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
   MessageBox.Show(((MyClass)autoCompleteBox1.SelectedItem).Organization_ID);
}

Upvotes: 1

Related Questions