내 문제는 ValueMemberPath에 같은 값을 가진 개체가있는 경우 AutoCompleteBox가 올바른 항목을 선택한 후 첫 번째 항목을 선택한다는 것입니다. SelectedItem을 Property에 바인딩했는데 동일한 값을 가진 항목이 여러 개인 경우 두 번 해고되는 것을 볼 수 있습니다.동일한 값을 가진 항목이 여러 개있을 때 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은 다음과 같습니다 : 나는 "당"쓸 때 4 개 항목이 드롭 다운에 표시됩니다
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;
}
. 이제 네 번째를 선택하면 선택되고 바인딩이 업데이트됩니다. 그러나 그런 다음 첫 번째 항목으로 돌아갑니다. 이 버그 또는 의도 된 동작이며 누구든지이 문제를 도와 줄 수 있습니까?
사용자가 어쨌든 차이점을 어떻게 알 수 있습니까? –
예, 사용자가 동일한 값의 목록에서 선택하는 방법을 설명 할 수 있다면 도움이 될 것이라고 생각합니다. –
우리가 더 많은 정보를 얻지 못한다면 말하기가 어렵습니다. 하지만이 코드를 올바르게 이해한다면 ValueMemberPath = "Name"은 ValueMemberPath = "ID"이어야합니다. – ingo