c# - WPF/XAML - 绑定(bind)到 TextBox PreviewTextInput 事件

标签 c# wpf xaml mvvm prism

我正在尝试将文本框“PreviewTextInput”绑定(bind)到 View 模型中的方法。 我正在关注 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.
    }

最佳答案

如果您添加对 Microsoft.Expressions.Interactions.dll 的引用(Project->Add Reference->Assemblies->Extensions in Visual Studio),您可以使用 CallMethodAction 调用一个方法:

<TextBox x:Name="txtServer" Text="{Binding Server, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
         xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="PreviewTextInput" >
            <ei:CallMethodAction TargetObject="{Binding}" MethodName="IsAllowedInput"  />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

当然方法不能是private:

public void IsAllowedInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
    //...
}

关于c# - WPF/XAML - 绑定(bind)到 TextBox PreviewTextInput 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50606518/

相关文章:

c# - 为什么 Func<> 委托(delegate)这么慢

c# - 悬停效果对WPF TreeView的子元素有错误

c# - UWP - 将自定义转换器应用于 x :Uid directive 设置的值

c# - 对 XamDataGrid 使用复杂字段

c# - 绑定(bind) Listview 的 GridView 的 DataGridCell 的 TextBlock

c# - asp.net中如何调用JS函数

c# - 如何清除字符串中的重复空格

c# - 获取通用枚举的描述属性

wpf - 如何使用带有 MVVM 模型的 WPF 数据绑定(bind)来使用自定义控件填充 StackPanel

WPF DatePicker 文本框白色边框不可编辑