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個項目將在下拉列表中顯示可以。現在當我選擇第四個時,它被選中並且綁定更新。然後,它會返回到第一個項目。這是一個錯誤或預期的行爲,有誰能幫我解決這個問題嗎?
無論如何,用戶希望能夠在這裏分辨出差異嗎? – 2011-03-02 16:13:42
是的,我想如果你能描述用戶應該如何從相同的值列表中選擇,這將是有益的。 – 2011-03-02 17:11:50
除非我們獲得更多信息,否則很難說。但是如果我理解你正確的話,這裏的代碼ValueMemberPath =「Name」應該是ValueMemberPath =「ID」 – ingo 2011-03-03 01:13:38