开发者

AutoCompleteBox issue when having multiple items with same values

My issue is that if I have objects with the same value in ValueMemberPath then the AutoCompleteBox selects the first item after it selects the correct item. I've bound SelectedItem to a Property and I can see it get's fired twice if there are multiple items with the same value.

I've bound my AutoCompleteBox to an ObservableCollection of Person objects.

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string FullName 
    {
        get
        {
            return Name + " - " + ID;
        }

    }
}

My XAML looks like this:

<StackPanel>
    <inputtoolkit:AutoCompleteBox x:Name="autoCompleteBox" ValueMemberPath="Name" ItemsSource="{Binding Persons}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}">
        <inputtoolkit:AutoCompleteBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FullName}" FontSize="14" FontWeight="Bold"></TextBlock>
            </DataTemplate>
        </inputtoolkit:AutoCompleteBox.ItemTemplate>
    </inputtoolkit:AutoCompleteBox>

    <TextBlock x:Name="textBlock" Text="{Binding SelectedPerson.ID}"></TextBlock>
</StackPanel>

My Window_Loaded looks like this:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Persons = new O开发者_运维知识库bservableCollection<Person>();

        Persons.Add(new Person() { ID = 1, Name = "Person" });
        Persons.Add(new Person() { ID = 2, Name = "Person" });
        Persons.Add(new Person() { ID = 3, Name = "Person" });
        Persons.Add(new Person() { ID = 4, Name = "Person" });

        autoCompleteBox.DataContext = this;
        textBlock.DataContext = this;
    }

When I write "Per" 4 items will be shown in the DropDown. Now when I selected the fourth, it gets selected and the binding updates. However it then goes back to the first item. Is this a bug or intended behaviour and can anyone help me with this issue?


I'm having this same issue. I haven't tried it yet, but I found this link and it looks to have solution.
http://www.yumasoft.com/node/45

Edit
I just confirmed that this does work.

For the comments asking how the user will tell the difference. The ItemTemplate providers more detail than is shown in just the TextBox portion. This allows the user to decide which record to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜