WPF 工具包 AutoCompleteBox 不刷新其数据

发布于 2024-10-24 22:47:44 字数 2212 浏览 1 评论 0原文

在自动完成框中,我选择“AAAA”女巫是我的人物集合中第一个人的名字。 之后,我按下按钮,将第一个人的名字更改为“Jeniffer”。 代码工作正常,但 AutocompleteBox 所选项目似乎没有刷新。

更改名称后如何使所选项目刷新?

在此处输入图像描述

XAML:

  <Grid Background="LightBlue" >
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Controls:AutoCompleteBox x:Name="autoCompleteBox" Populating="autoCompleteBox_Populating" Height="30">
    </Controls:AutoCompleteBox>
    <Button Grid.Row="1" Click="Button_Click">Change first person Name</Button>
</Grid>

代码隐藏:

 public partial class Window1 : Window {
    public delegate void SearchResults(IEnumerable results);
    public ObservableCollection<Person> Persons { get; set; }

    public Window1() {
        Persons = new ObservableCollection<Person> {
                                       new Person {Name = "AAAA"},
                                       new Person {Name = "BBBB"},
                                       new Person {Name = "CCCC"},
                                   };
        InitializeComponent();
    }

    private void autoCompleteBox_Populating(object sender, PopulatingEventArgs e) {
        e.Cancel = true;
        Search(autoCompleteBox.SearchText, delegate(IEnumerable results) {
            autoCompleteBox.ItemsSource = results;
            autoCompleteBox.PopulateComplete();
        });
    }

    private void Search(string phrase, SearchResults resultsDelegate) {
        var results = new ObservableCollection<Person>();
        foreach (var person in Persons) {
            if (person.Name.ToLower().Contains(phrase.ToLower())) {
                results.Add(person);
            }
        }

        resultsDelegate(results);
    }

    private void Button_Click(object sender, RoutedEventArgs e) {
        Persons.FirstOrDefault(p => p.Name == "Aaaaa").Name = "Jennifer";
    }
}

public class Person {
    public string Name { get; set; }
    public override string ToString() {
        return Name;
    }
}

In the AutoCompleteBox I choose "AAAA" witch is the name of the first person in my Persons collection.
After that I press the button that change the first person's name to "Jeniffer".
The code is working fine but the AutocompleteBox selected item doesn't seem to be refreshed.

How can I make the selected item refresh after changing the name ?

enter image description here

XAML:

  <Grid Background="LightBlue" >
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Controls:AutoCompleteBox x:Name="autoCompleteBox" Populating="autoCompleteBox_Populating" Height="30">
    </Controls:AutoCompleteBox>
    <Button Grid.Row="1" Click="Button_Click">Change first person Name</Button>
</Grid>

CodeBehind:

 public partial class Window1 : Window {
    public delegate void SearchResults(IEnumerable results);
    public ObservableCollection<Person> Persons { get; set; }

    public Window1() {
        Persons = new ObservableCollection<Person> {
                                       new Person {Name = "AAAA"},
                                       new Person {Name = "BBBB"},
                                       new Person {Name = "CCCC"},
                                   };
        InitializeComponent();
    }

    private void autoCompleteBox_Populating(object sender, PopulatingEventArgs e) {
        e.Cancel = true;
        Search(autoCompleteBox.SearchText, delegate(IEnumerable results) {
            autoCompleteBox.ItemsSource = results;
            autoCompleteBox.PopulateComplete();
        });
    }

    private void Search(string phrase, SearchResults resultsDelegate) {
        var results = new ObservableCollection<Person>();
        foreach (var person in Persons) {
            if (person.Name.ToLower().Contains(phrase.ToLower())) {
                results.Add(person);
            }
        }

        resultsDelegate(results);
    }

    private void Button_Click(object sender, RoutedEventArgs e) {
        Persons.FirstOrDefault(p => p.Name == "Aaaaa").Name = "Jennifer";
    }
}

public class Person {
    public string Name { get; set; }
    public override string ToString() {
        return Name;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2024-10-31 22:47:44

最简单的方法是更改​​ Text 属性:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Persons.FirstOrDefault(p => p.Name == "AAAA").Name = "Jennifer";
        if(autoCompleteBox.Text == "AAAA")
            autoCompleteBox.Text = "Jennifer";
    }

因为文本字段与 SelectedItem 属性无关,所以它只与 Text 属性相关。

The easiest way is to change the Text property:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Persons.FirstOrDefault(p => p.Name == "AAAA").Name = "Jennifer";
        if(autoCompleteBox.Text == "AAAA")
            autoCompleteBox.Text = "Jennifer";
    }

Because the text field has nothing to do with the SelectedItem property, it is connected only with the Text property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文