c# - 检测文本框 mvvm windows phone 8 中更改的文本?

标签 c# mvvm windows-phone-8

使用 mvvm 时,如何在 windows phone 8 应用程序的文本框中输入文本时检测更改,但请注意我没有使用 mvvmlight。

我试过使用interaction.Triggers,虽然每次我输入一些文本时都会触发事件,但绑定(bind)到控件的模型不会在我的 View 模型中设置控件的Text属性。

我的 viewModel 有一个允许访问实际模型的属性,通常我会有:

<TextBox Text={Binding Person.Title, Mode=TwoWay}"

但为了测试,我刚刚在我的 ViewModel 中直接创建了一个 Title 属性,但行为相同。

我很确定 Text 属性已正确绑定(bind),因为当我的 View 被初始化并且确实被调用时,我已经放置了断点。

这是我拥有的代码,每次输入时都会触发一个事件,但不幸的是,我的标题似乎永远不会被设置。
<TextBox Text="{Binding Title, Mode=TwoWay}" Grid.Row="1">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="TextChanged">
      <i:InvokeCommandAction Command="{Binding TitleTextChanged, Mode=OneWay}" CommandParameter="{Binding}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</TextBox>

有任何想法吗?

谢谢。

更新:

更改的属性肯定是从我的 ViewModel 提出的
public string Title
{
    get { return this._title; }
    set { if (this._title != value) this.SetProperty(ref this._title, value); }
}

但正如最初提到的,我只添加了这个属性来测试属性更改事件,因为我通常会通过我的 ViewModel 以这种方式访问​​我的模型对象:
public PersonModel CurrentPerson
{
    get { return this._currentPerson; }
    set { if (this._currentPerson != value) this.SetProperty(ref this._currentPerson, value);}
}

我希望这能澄清一些事情。

谢谢。

最佳答案

<TextBox Text="{Binding TextInViewModel, Mode=TwoWay, 
                UpdateSourceTrigger=PropertyChanged}"/>

关于c# - 检测文本框 mvvm windows phone 8 中更改的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20028058/

相关文章:

c# - var foo = new Love(); 有什么区别? AND object foo = new Love();?

c# - WPF UI不刷新

c# - 将 SelectedItem 传递给 DataTemplate 中 UserControl 的 ViewModel

android - Html5输入类型数字格式化

c# - 为什么 `j=++i+++i;` 在 C# 和 C 中的输出不同?

c# - 将 Viewmodel 对象发送到 ASP .Net MVC 中的 Razor View

c# - "Dictionary"键作为存储元素的一部分?

c# - 绑定(bind)与 x :Bind: Why isn't the view updated when using Binding?

iphone - Windows Phone 以编程方式连接两个设备并镜像屏幕

database - Windows Phone 8 SQLite 数据库升级