【发布时间】:2018-11-09 10:00:50
【问题描述】:
我正在尝试将文本框“PreviewTextInput”绑定到视图模型中的方法。 我正在关注this article 但是我的方法永远不会被调用。 这是我在 XAML 中的代码:
<UserControl x:Class="ConfigurationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OPCUAProjectModule.Views"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="700">
.....
.....
.....
<TextBox x:Name="txtServer" Text="{Binding Server, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewTextInput" >
<i:InvokeCommandAction Command="{Binding IsAllowedInput}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
....
....
这里我们使用 ViewModel 代码:
public class ConfigurationViewModel : BindableBase, INotifyDataErrorInfo
{
....
....
public string Server
{
get
{
return this.server;
}
set
{
this.SetProperty(ref this.server, value);
}
}
private void IsAllowedInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
//Never enters here.
}
【问题讨论】:
-
IsAllowedInput是一种方法。绑定不适用于方法。你需要一个公共的 ICommand 属性来进行命令绑定