首页 > 其他分享 >How can I access the UI element AutoSuggestEdit on wpf in model? I need reload the item source of Au

How can I access the UI element AutoSuggestEdit on wpf in model? I need reload the item source of Au

时间:2023-03-07 12:44:17浏览次数:55  
标签:source Suggestions reload AutoSuggestEdit property model view

How can I access the UI element AutoSuggestEdit on wpf in model? I need reload the item source of AutoSuggestEdit when input text changed

In the MVVM pattern, it's generally not recommended to access UI elements directly from the model layer. Instead, you can create a property in the view model that will hold the item source for the AutoSuggestEdit control. Then, you can bind this property to the AutoSuggestEdit's ItemsSource property in the view.

To reload the item source when the text changes, you can subscribe to the TextChanged event of the AutoSuggestEdit control in the view, and call a command in the view model that will update the item source. Here's an example:

  1. Create a property in the view model that will hold the item source:
public ObservableCollection<string> Suggestions { get; set; }

2.In the view, bind the ItemsSource property of the AutoSuggestEdit control to the Suggestions property of the view model:

<dxe:AutoSuggestEdit ItemsSource="{Binding Suggestions}" />

3.Subscribe to the TextChanged event of the AutoSuggestEdit control in the view, and call a command in the view model that will update the Suggestions property:

private void AutoSuggestEdit_TextChanged(object sender, RoutedEventArgs e)
{
    var vm = DataContext as MyViewModel;
    vm.UpdateSuggestionsCommand.Execute(AutoSuggestEdit.Text);
}

4.In the view model, create a command that will update the Suggestions property:

public ICommand UpdateSuggestionsCommand { get; }

private void UpdateSuggestions(string searchText)
{
    // Call your service to get suggestions based on the search text
    Suggestions.Clear();
    Suggestions.Add("Suggestion 1");
    Suggestions.Add("Suggestion 2");
    Suggestions.Add("Suggestion 3");
}

You can use a RelayCommand or DelegateCommand to implement the UpdateSuggestionsCommand. Note that in the example above, the UpdateSuggestions method is called with the search text as a parameter. You can use this parameter to query your service and update the Suggestions property accordingly.

 

也可以给AutoSuggestEdit这个控件的EditValue绑定Email属性。

 public string Email
        {
            get => GetValue<string>();
            set
            {
                SetValue(value,OnQuerySubmitted);
            }
        }

 

标签:source,Suggestions,reload,AutoSuggestEdit,property,model,view
From: https://www.cnblogs.com/chucklu/p/17187659.html

相关文章