2011-03-02 90 views
3

我的问题是,如果ValueMemberPath中具有相同值的对象,则AutoCompleteBox会在选择正确的项目后选择第一个项目。 我已经将SelectedItem绑定到一个属性,我可以看到它被触发两次,如果有多个具有相同值的项目。具有相同值的多个项目时出现AutoCompleteBox问题

我已经将我的AutoCompleteBox绑定到Person对象的ObservableCollection。

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

    } 
} 

我的XAML看起来像这样:

<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> 

我Window_Loaded看起来是这样的:

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     Persons = new ObservableCollection<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; 
    } 

当我写 “每” 4个项目将在下拉列表中显示可以。现在当我选择第四个时,它被选中并且绑定更新。然后,它会返回到第一个项目。这是一个错误或预期的行为,有谁能帮我解决这个问题吗?

+1

无论如何,用户希望能够在这里分辨出差异吗? – 2011-03-02 16:13:42

+0

是的,我想如果你能描述用户应该如何从相同的值列表中选择,这将是有益的。 – 2011-03-02 17:11:50

+1

除非我们获得更多信息,否则很难说。但是如果我理解你正确的话,这里的代码ValueMemberPath =“Name”应该是ValueMemberPath =“ID” – ingo 2011-03-03 01:13:38

回答

1

我有这个相同的问题。我还没有尝试过,但我发现这个链接,它看起来有解决方案。
http://www.yumasoft.com/node/45

编辑
我只是确认这确实工作。

询问用户如何分辨差异的评论。 ItemTemplate提供了比仅在TextBox部分中显示的更多细节。这允许用户决定使用哪个记录。

相关问题