GridView WPF MVVM中TextBox的TextChanged事件

编程入门 行业动态 更新时间:2024-10-25 16:24:14
本文介绍了GridView WPF MVVM中TextBox的TextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对MVVM模式很陌生。我有一个表单,它包含一个 TextBox 和一个 DataGrid 。我的 DataGrid 与 ObservableCollection 绑定。我希望能够使用 TextChanged 事件 TextBox 进行搜索,并在 DataGrid 。

在<$ $中使用 TextBox c $ c> GridView 及其在模型视图模型中。基本上,我想要做的是每次编辑框中的文本时调用方法。那就是当文本输入时,函数会调用。这是文本更改事件应该工作。但是在模型视图模型中,我能做什么?请帮助我。任何想法....

解决方案

您在绑定到属性的setter中触发函数文本框。您还必须将绑定的UpdateSourceTrigger设置为PropertyChanged,以便在您每次更改文本框的内容时触发它。

setter中触发的函数应更新ObservableCollection,它将导致DataGrid更新其内容。

在下面的代码示例中,代码不会编译,但会显示一般想法。 b xaml:

< DataGrid ItemsSource ={Binding Rows}AutoGenerateColumns =False > < DataGrid.Columns> < DataGridTemplateColumn> < DataGridTemplateColumn.CellTemplate> < DataTemplate> < TextBox Text ={Binding Text}/> < / DataTemplate> < /DataGridTemplateColumn.CellTemplate> < / DataGridTemplateColumn> < /DataGrid.Columns> < / DataGrid>

SubviewModel.cs:

public SubViewModel(ViewModel vm) { _vm = vm; } private string _text; 私人ViewModel _vm; public string Text { get { return _text; } set { if(_text == value) { return; } _text = value; OnPropertyChanged(Text); RefreshResult(); } private void RefreshResult() { //用_text做一些操作并操作_vm.Rows? }

ViewModel.cs:

private ObservableCollection< SubViewModel> _rows; public ViewModel() { //初始化子视图模型 _rows = new ObservableCollection< SubViewModel>(); //以某种方式填充列表 foreach(var in sourceOfRows) { _rows.Add(new SubViewModel(this)); } } public ObservableCollection< SubViewModels>行 {获得 { return _rows; } }

I am new to the MVVM pattern. I have a form, that includes one TextBox, and one DataGrid. My DataGrid binding with an ObservableCollection. What I would like is to be able to have the search with TextChanged event of TextBox and show result in DataGrid.

I'm using a TextBox in the GridView and its in Model View-View Model. Basically, what I want to do is call a method every time the text in the box is edited. That is when the text is entered, the function will call. That is the text change event should work. But in Model View-View Model, what can I do? Please help me.Any idea....

解决方案

You trigger the function in the setter of the property that you bind to the textbox. You also have to set UpdateSourceTrigger of the binding to PropertyChanged in order to get it to trigger everytime you change the content of the textbox.

The function triggered in the setter should update the ObservableCollection, which will cause the DataGrid to update its content.

Se the code example below, code wont compile but shows the general idea.

xaml:

<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="False" > <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Text}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>

SubviewModel.cs:

public SubViewModel(ViewModel vm) { _vm = vm; } private string _text; private ViewModel _vm; public string Text { get { return _text; } set { if (_text == value) { return; } _text = value; OnPropertyChanged("Text"); RefreshResult(); } private void RefreshResult() { // Do something with the _text and manipulate the _vm.Rows? }

ViewModel.cs:

private ObservableCollection<SubViewModel> _rows; public ViewModel() { //Initialize the sub view models _rows = new ObservableCollection<SubViewModel>(); //Populate the list somehow foreach (var r in sourceOfRows) { _rows.Add(new SubViewModel(this)); } } public ObservableCollection<SubViewModels> Rows { get { return _rows; } }

更多推荐

GridView WPF MVVM中TextBox的TextChanged事件

本文发布于:2023-11-12 05:06:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   WPF   GridView   MVVM   TextChanged

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!