gpt4 book ai didi

c# - 具有多个具有相同值的项目时的 AutoCompleteBox 问题

转载 作者:行者123 更新时间:2023-11-30 18:05:50 26 4
gpt4 key购买 nike

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

我已将我的 AutoCompleteBox 绑定(bind)到 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;
}

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

最佳答案

我也遇到了同样的问题。我还没有尝试过,但我找到了这个链接,它看起来有解决方案。
http://www.yumasoft.com/node/45

编辑
我刚刚确认这确实有效。

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

关于c# - 具有多个具有相同值的项目时的 AutoCompleteBox 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com