开发者

WPF tool kit AutoCompleteBox doesn't refresh its data

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 ?

WPF tool kit AutoCompleteBox doesn't refresh its data

XAML:

  <Grid Background="LightBlue" >
 开发者_JS百科   <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;
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜