Autocomplete text in Silverlight
I'm having trouble loading the autocomplete block in System.Windows.Controls.Input working as it sees fit. When I start typing the dropdown that displays the filtered list does not show the property that I am binding to, the class name is displayed instead.
So in the example below, when I type text, instead of showing "My Name", it shows MyNamespace.Person. However, when I select an item from the autocomplete list, it displays the FullName property in the textbox. I'm sure I just missed just the autocomplete box property, but I can't see it.
Sample code:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
}
In my xaml code behind I create some Person objects and store them in a list and bind this list to the autocomplete block
List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "My", LastName = "Name" });
people.Add(new Person { FirstName = "Fernando", LastName = "Torres" });
acbNames.ItemsSource = people;
My xaml:
<my:AutoCompleteBox Name="acbNames" ValueMemberPath="FullName" />
/ * after entering "my", automatically ends displaying "MyNamespace.Person" instead of "My Name", but displays "My Name" after selecting an item from the list * /
a source to share
It turns out I need to use the ItemTemplate for the dropdown of the AutoCompleteBox, so now the xaml for it will be:
<my:AutoCompleteBox Name="acbNames" ValueMemberBinding="{Binding FullName}">
<my:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FullName}"/>
</DataTemplate>
</my:AutoCompleteBox.ItemTemplate>
</my:AutoCompleteBox>
a source to share
Yes, your problem was that you didn't place the element template. But if you put an element template and still get the problem, read what happened in Sandro.
I had the same problem. I decided it to use Static resource for control style
This is the style I used:
<Style x:Key="autocomplete" TargetType="sdk1:AutoCompleteBox">
<Setter Property="Margin" Value="5,0,5,0"/>
<Setter Property="MinWidth" Value="100"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property ="HorizontalAlignment" Value="Right"/>
</Style>
If I don't use this style, my custom is not showing correctly as I am setting in the DataItem, it shows the class name instead.
share | change
This works for me too, but only when I applied some custom theme style from the toolkit. When using a theme from the toolkit
there are other ways to work around the problem:Best,
debarisi
a source to share
I had the same problem. I decided it to use Static resource for control style
This is the style I used:
<Style x:Key="autocomplete" TargetType="sdk1:AutoCompleteBox">
<Setter Property="Margin" Value="5,0,5,0"/>
<Setter Property="MinWidth" Value="100"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property ="HorizontalAlignment" Value="Right"/>
</Style>
If I don't use this style, my custom is not showing correctly as I am setting in the DataItem, it shows the class name instead.
a source to share