wpf - WPF:MVVM Microsoft.Expression.Interaction
问题描述
目的是在文本框中输入的内容,相同的内容将显示在TextBlock
.
交互触发将是PreviewTextInput
文本框的完成事件。
但它给出了错误但没有按预期工作。
已附加运行时图像的绑定错误。
下面是代码。
XAML
<Window x:Class="MVVMApp.TextBindings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMApp"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="Text Bindings" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Enter Text Value" Grid.Row="0" Grid.Column="0"/>
<TextBox Name="txtBox1" Text="{Binding BxVal,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" Grid.Column="1" Width="150" Height="30">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewTextInput">
<i:InvokeCommandAction Command="{Binding cmdType}" CommandParameter="{Binding BxVal,ElementName=txtBox1}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<TextBlock Text="Result --> " Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="{Binding Result,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Lavender" Grid.Row="1" Grid.Column="1"/>
</Grid>
MVVM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVVMApp.Helper;
namespace MVVMApp.ViewModel
{
class TextBindingsVM :ViewModelBase
{
DelegateCommand <object> cmdType { get; set; }
private string _Result { get; set; }
public string Result
{
get { return _Result; }
set
{
_Result = value;
OnPropertyChanged(this, "Result");
}
}
private string _BxVal { get; set; }
public string BxVal
{
get { return _BxVal; }
set
{
_BxVal = value;
OnPropertyChanged(this, "BxVal");
Result = BxVal;
}
}
public TextBindingsVM()
{
cmdType = new DelegateCommand<object>(cmdType_Execute);
}
private void cmdType_Execute(object obj)
{
throw new NotImplementedException();
}
}
}
解决方案
我发现了这个问题。实际上 INotifyPropertyChanged 没有在 ViewModelBase 类中实现
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMApp.Helper
{
class ViewModelBase:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(object sender, string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
}
推荐阅读
- opengl - LWJGL/OpenGL 当我将纹理映射到半球体或球体时,为什么我的纹理边缘会出现点?
- ios - UIView 上的 CollectionView 单元格实例(委托外部)无法快速运行 5
- python - 组权限的Django TestCase?
- oracle - SQL 错误:ORA-00984:此处不允许列 00984。00000 -“此处不允许列”
- qt - QMenu 右对齐文本
- c# - 将点击事件分配给 DataTemplate 中的按钮
- javascript - 使用Angularjs以编程方式通过名称将表单外的输入发布到表单
- r - ggsave scale 不缩放文本大小
- swift - 我收到一个错误:使用未解析的标识符“UINotificationFeedbackGenerator”
- sas - 多元线性回归、分类和数值自变量