GridView WPF MVVM中TextBox的TextChanged事件 [英] TextChanged event of TextBox in GridView WPF MVVM

查看:1755
本文介绍了GridView WPF MVVM中TextBox的TextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVVM模式很陌生。我有一个表单,它包含一个 TextBox 和一个 DataGrid 。我的 DataGrid ObservableCollection 绑定。我希望能够使用 TextChanged 事件 TextBox 进行搜索,并在<$ c中显示结果$ c $> 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事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆