0

I am working on MVVM , I have used interactions. XAML Binding

Purpose is what ever will be typed in text box, same will be displayed in TextBlock.

Interaction trigger will be done event PreviewTextInput of text box.

But it is giving error but not working as expected.

Binding error at runtime image has been attached.

Below is the code.

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();
    }
}

}

EldHasp
  • 6,079
  • 2
  • 9
  • 24
  • I see no 'binding error at runtime' image – mcalex Jul 06 '21 at 06:18
  • Sorry,i have added image now, XAML Binding – Ajay Mishra Jul 06 '21 at 07:25
  • Change your binding CommandParameter to "{Binding DataContext.BxVal,ElementName=txtBox1}. Also you do not required event trigger for this specific scenario, result text can be binded directly to inpurt text – Vimal CK Jul 06 '21 at 07:46
  • 1
    No offense, but you just have a dump of meaningless code. For example, you set the binding `{Binding BxVal, ElementName = txtBox1}`, but the TextBox does not have the `BxVal` property! Or you have specified in the command the execution of the `cmdType_Execute` method, but this method has nothing but throwing an exception! It will be much easier if, instead of explaining **HOW** you implement the task, you describe **WHAT** you want to implement. – EldHasp Jul 06 '21 at 07:49
  • After changing command parameter tro DataContext.BxVal the error is gone, but delegate command i.e. cmdtype still it showing error in Bindingpath – Ajay Mishra Jul 06 '21 at 07:59
  • The expected output is,The Text which is being typed in textbox should be displayed in text block,which is not working. – Ajay Mishra Jul 06 '21 at 09:52

1 Answers1

0

I found the issue. Actually INotifyPropertyChanged was not implemented in ViewModelBase class

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));
            }
        }
    }
}