WPF 工具包 AutoCompleteBox 不刷新其数据
在自动完成框中,我选择“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 ?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是更改 Text 属性:
因为文本字段与 SelectedItem 属性无关,所以它只与 Text 属性相关。
The easiest way is to change the Text property:
Because the text field has nothing to do with the SelectedItem property, it is connected only with the Text property.