Autocompletebox text in Silverlight
I'm having trouble getting the autocomplete box in System.Windows.Controls.Input working as I wish. When I start typing the dropdown section that displays the filtered list doesn't show the property that I'm binding to, it shows the class name instead.
So in the example below, when I type in my - instead of showing 'My Name' it shows MyNamespace.Person. However, when I select the item from the autocomplete list, it displays the FullName property in the textbox. I'm sure I'm just missing a simple autocomplete box property somewhere but I can't see it.
Example 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 that list to an autocomplete box
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', auto complete displays 'MyNamespace.Person' instead of 'My Name', but displays 'My Name' after selecting the item from the list */
It turns out I need to use an ItemTemplate for the dropdown part of the AutoCompleteBox, so the xaml for it would now be as follows:
<my:AutoCompleteBox Name="acbNames" ValueMemberBinding="{Binding FullName}">
<my:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FullName}"/>
</DataTemplate>
</my:AutoCompleteBox.ItemTemplate>
</my:AutoCompleteBox>
Yes, your problem was because you didn't put item template. But if you put item template and still got problem read what Sandro has wroted.
I had same problem. I solved it using a Static resource for the 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 Customs Item are not displayed correctly as I configure in DataItem, instead it show the Class name.
share|edit
This works for me too but only when i applied some custom theme style from toolkit. There are some other workarounds when you use theme from toolkit
Best,
debarisi
I had same problem. I solved it using a Static resource for the 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 Customs Item are not displayed correctly as I configure in DataItem, instead it show the Class name.
精彩评论