Abdulsalam Elsharif
Abdulsalam Elsharif

Reputation: 5101

How to Set Item source for WPF toolkit auto complete box inside dataTemplate

I have the following ListBox

<ListBox x:Name="lbListItems"
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ToggleButton x:Name="btnItem">
                    <StackPanel Orientation="Horizontal" Width="956">
                        <toolkit:AutoCompleteBox x:Name="acbItem"</toolkit:AutoCompleteBox>
                    </StackPanel>
                </ToggleButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

How can I set the ItemsSource for acbItem in code behind ? I cant access to it !

Upvotes: 0

Views: 298

Answers (2)

mm8
mm8

Reputation: 169200

I bind the ItemsSource property for the Listbox but it does not work, If I put the Listbox out of template it works !

Then you should specify a RelativeSource of the binding:

<toolkit:AutoCompleteBox x:Name="acbItem" 
   ItemsSource="{Binding DataContext.YourItemsSourceCollection, RelativeSource={RelativeSource AncestorType=ListBox}}" />

Simply replace "YourItemsSourceCollection" with the actual name of the propertty to bind to and keep the rest.

Upvotes: 0

Antoine V
Antoine V

Reputation: 7204

Use Loaded event is a solution.

<toolkit:AutoCompleteBox Loaded="myControl_Loaded" ...

private void myControl_Loaded(object sender, RoutedEventArgs e)
{
    toolkit:AutoCompleteBox myCombo = sender as toolkit:AutoCompleteBox; 
    // Do things..
}

But it's better to use MVVM approach to set ItemsSource from XAML

Upvotes: 1

Related Questions