How can I access the UI element AutoSuggestEdit on wpf in model? I need reload the item source of AutoSuggestEdit when input text changed
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:
- 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);
}
}
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-03-07 上海疫情防控级别
2020-03-07 面板评分太低会算两次
2020-03-07 没有使用大漩涡传送门没有杀死大法师瓦格斯
2019-03-07 decimal and double ToString problem
2019-03-07 github上的markdown如何换行
2019-03-07 卸载vs2017
2017-03-07 Chocolatey