locked
AutoCompleteBox ... "Expression.Blend.SampleData.Users.CollectionItem"? RRS feed

  • Question

  • I am testing the AutoCompleteBox control but I can't seem to get the control to use my test data.  I get in all rows "Expression.Blend.SampleData.Users.CollectionItem" instead of my data when I start typing?
    Monday, February 22, 2010 2:29 PM

All replies

  • By default the autocomplete box uses .ToString() on the objects to determine what it displays. What you are seeing is the default ToString() method returning the type name of the object.

    If you controlled the data type class, you could override the ToString() method to return what you want, but since you don't (Blend will overwrite it if you change anything in sample data), there are 2 other options.

    The simplest is to do this:
    <UserControl.Resources>
    	<SampleData:SampleDataSource x:Key="SampleDataSource" d:IsDataSource="True"/>
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
    	<input:AutoCompleteBox ItemsSource="{Binding Collection}" ValueMemberPath="Property1">
    		<input:AutoCompleteBox.ItemTemplate>
    			<DataTemplate>
    				<TextBlock Text="{Binding Property1}" />
    			</DataTemplate>
    		</input:AutoCompleteBox.ItemTemplate>
    	</input:AutoCompleteBox>
    </Grid>
    The key parts being the ValueMemberPath, and the data template to show the correct property.

    The other option is to use ValueMemberBinding, and possible write your own IValueConverter to return what you want. 
    Monday, February 22, 2010 3:49 PM
    Moderator
  • Hi Chuck, just got around trying this one out and I can't seem to get it to work.  I have 3 sample data sources in my project.  Using a Combobox it works fine by just dropping the sample data on the ComboBox control but it doesn't work at all using an AutoCompleteBox.

    I tried to bind the sample data using the AutoCompleteBox's Common Properties - Data Context - Data Binding ... and then browse to the required propery.
    <UserControl.Resources>
    	<ResourceDictionary>
    		<ResourceDictionary.MergedDictionaries>
    			<ResourceDictionary Source="ProjectDataSources.xaml"/>
    		</ResourceDictionary.MergedDictionaries>
    		<DataTemplate x:Key="ItemTemplate">
    			<StackPanel>
    				<TextBlock Text="{Binding UserName}"/>
    			</StackPanel>
    		</DataTemplate>
    	</ResourceDictionary>
    </UserControl.Resources>

    <input:AutoCompleteBox HorizontalAlignment="Left" Margin="412,0,0,137" VerticalAlignment="Bottom" FontSize="18.667" Width="200" ItemsSource="{Binding Collection}" IsTextCompletionEnabled="True" FilterMode="Contains" DataContext="{Binding Collection[0].UserName, Mode=OneWay}"/>



    Thursday, February 25, 2010 11:44 AM