首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF : MVVM Microsoft.Expression.Interaction

WPF : MVVM Microsoft.Expression.Interaction
EN

Stack Overflow用户
提问于 2021-07-06 05:46:18
回答 1查看 100关注 0票数 0

我正在研究MVVM,我使用了交互。

目的是在文本框中输入任何内容,在TextBlock中也会显示同样的内容。

交互触发器将完成文本框的事件PreviewTextInput

但它带来了错误,但并不像预期的那样起作用。

已附加了运行时映像中的绑定错误。

下面是密码。

XAML

代码语言:javascript
复制
<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

代码语言:javascript
复制
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();
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2021-07-11 08:40:55

我发现了问题。实际上,INotifyPropertyChanged没有在ViewModelBase类中实现。

代码语言:javascript
复制
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));
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68265160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档